62 lines
2 KiB
Python
62 lines
2 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
import random
|
||
|
import time
|
||
|
import argparse
|
||
|
import matplotlib.pyplot
|
||
|
|
||
|
def draw_sample(limit = 45, count = 7, system = False):
|
||
|
rand = random.SystemRandom() if system else random
|
||
|
return rand.sample(range(1, limit + 1), count)
|
||
|
|
||
|
def generate_stats(results):
|
||
|
numbers = {}
|
||
|
|
||
|
for result in results:
|
||
|
for number in result:
|
||
|
try:
|
||
|
numbers[number] += 1
|
||
|
except KeyError:
|
||
|
numbers[number] = 1
|
||
|
|
||
|
return numbers
|
||
|
|
||
|
def run_draws(draws = 1, benchmark = False, statistics = False, *args, **kwargs):
|
||
|
results = []
|
||
|
start = time.time()
|
||
|
|
||
|
for __ in range(0, draws):
|
||
|
results += [draw_sample(*args, **kwargs)]
|
||
|
|
||
|
end = time.time()
|
||
|
|
||
|
if statistics:
|
||
|
results = generate_stats(results)
|
||
|
|
||
|
return (end - start) if benchmark else results
|
||
|
|
||
|
def generate_plot(results, outfile):
|
||
|
matplotlib.pyplot.bar(list(results.keys()), results.values())
|
||
|
matplotlib.pyplot.savefig(outfile)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
parser = argparse.ArgumentParser()
|
||
|
parser.add_argument("--count", "-c", type=int, help="number of numbers per draw", default=7)
|
||
|
parser.add_argument("--draws", "-d", type=int, help="number of draws to simulate", default=1)
|
||
|
parser.add_argument("--limit", "-l", type=int, help="highest number to be drawn (lowest is always 1)", default=45)
|
||
|
parser.add_argument("--system", "-s", action="store_true", help="use more secure source of randomness (considerably slower)")
|
||
|
|
||
|
formats = parser.add_mutually_exclusive_group()
|
||
|
formats.add_argument("--benchmark", "-b", action="store_true", help="output time needed to process draws instead of numbers drawn")
|
||
|
formats.add_argument("--analysis", "-a", action="store_true", help="output statistics about draws instead of numbers drawn")
|
||
|
formats.add_argument("--graph", "-g", type=str, help="generate chart in file with statistics about draws instead of numbers drawn", metavar="FILE")
|
||
|
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
output = run_draws(args.draws, args.benchmark, args.analysis or not not args.graph, args.limit, args.count, args.system)
|
||
|
|
||
|
if args.graph:
|
||
|
generate_plot(output, args.graph)
|
||
|
else:
|
||
|
print(output)
|