Skip to content

Instantly share code, notes, and snippets.

@zthomae
Created August 19, 2012 01:21
Show Gist options
  • Select an option

  • Save zthomae/3390773 to your computer and use it in GitHub Desktop.

Select an option

Save zthomae/3390773 to your computer and use it in GitHub Desktop.
Gen Tester - See how many random numbers need to be generated to have created every number in range n
#!/usr/bin/python2.7
import pickle, os, sys
from sets import Set
from random import randrange
from shutil import make_archive, rmtree
from time import time
def SetGenCount(input_length):
"""
The core process. Generates a set of numbers from 0 to input_length, and, while the master set is not empty, iterates through a process in which:
1) The step counter is incremented.
2) A random number is generated within the set range.
3) If this random number is present in the set, it is removed.
This process terminates when the set is empty (meaning that all of the numbers in the range have been generated), at which point the counter is returned.
"""
length = input_length + 1
master = Set(range(0, length, 1))
count = 0
while master:
count += 1
rand = randrange(0, length, 1)
if rand in master:
master.remove(rand)
else:
pass
return count
def GenTester(length, attempts):
"""
Runs SetGenCount 1000 times for a given length, putting the returned values into a list.
The list is then pickled into a text file in trials/, named <length>raw.txt, for later use.
"""
try:
raw_file = open(os.path.join('trials', str(length)+'_'+str(attempts)+'raw.txt'), 'w')
except IOError:
os.makedirs('trials')
raw_file = open(os.path.join('trials', str(length)+'_'+str(attempts)+'raw.txt'), 'w')
results = []
for i in range(attempts):
out = SetGenCount(length)
results.append(out)
pickle.dump(results, raw_file)
raw_file.close()
def ListAverage(list):
"""
Averages a list of numbers.
"""
length = len(list)
total = 0
for i in range(length):
total = total + list[i]
average = total / length
return average
def AvgData(length, attempts):
"""
Averages the raw data output from GenTester() of a given length. Output returned, for use in other functions.
"""
raw_file = open(os.path.join('trials', str(length)+'_'+str(attempts)+'raw.txt'), 'r')
raw = pickle.load(raw_file)
avg = ListAverage(raw)
raw_file.close()
return avg
def DataSet(num, attempts):
"""
Generates a data set of averages for a given number of trials.
0) If it exists, backs up and removes the existing information in the working trials/ directory (<time>trials.tar.bz2).
1) Runs GenTester for the numbers 1-num.
2) Averages the data, and enters the results into a dictionary {<length> : <average>}
3) Pickles the set into a file.
"""
try:
os.makedirs('archives')
except OSError:
pass
try:
os.makedirs('trials')
except OSError: ## If the directory exists, then backs up information
archive_name = os.path.expanduser(os.path.join(str(os.getcwd()), 'archives', str(time()) + 'archive'))
root_dir = os.path.expanduser(os.path.join(str(os.getcwd()), 'trials'))
make_archive(archive_name, 'bztar', root_dir)
rmtree(os.path.join(str(os.getcwd()), 'trials'))
os.makedirs('trials')
avg_file = open(os.path.join('trials', 'avg.txt'), 'w')
average_set = {}
for i in range(1, num + 1, 1):
GenTester(i, attempts)
average_set[i] = AvgData(i, attempts)
pickle.dump(average_set, avg_file)
avg_file.close()
print(average_set)
return average_set
def ShellGenTest():
"""
A shell process for DataSet, used when the input_length value is given by the user.
"""
try:
length = input('How many numbers? ')
attempts = input('How many tests? ')
DataSet(length, attempts)
except NameError:
print('You must enter a number!')
if __name__ == "__main__":
if len(sys.argv) >= 3:
DataSet(int(sys.argv[1]), int(sys.argv[2]))
elif len(sys.argv) == 2:
DataSet(int(sys.argv[1]), 1000)
else:
ShellGenTest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment