pickrand.py: seed RNG with process ID
[cmccabe-bin] / pickrand.py
1 #!/usr/bin/python
2
3 import os
4 import random
5 import sys
6
7 print_to_stderr = False
8 if (len(sys.argv) == 1):
9     pass
10 elif (len(sys.argv) == 2) and (sys.argv[1] == "-S"):
11     print_to_stderr = True
12 else:
13     print >>sys.stderr, "invalid command-line arguments"
14     sys.exit(1)
15
16 allfiles = []
17
18 for root, dirs, files in os.walk("."):
19     for f in files:
20         allfiles.append(os.path.join(root, f))
21 if (len(allfiles) == 0):
22    sys.exit(1)
23 random.seed(os.getpid())
24 r = random.randint(0,len(allfiles) - 1)
25 print(allfiles[r])
26 if (print_to_stderr):
27     print >>sys.stderr, (allfiles[r])
28 sys.exit(0)