I want to check the central limit with dices. Roll D dices. Sum the results. Repeat the same thing for N times. Change D and repeat.
There's no need to store random values so I want to use only generators. The problem is that the generators are consumed; I can't reuse them many times. Now my code uses explicit for
and I don't like it.
dice_numbers = (1, 2, 10, 100, 1000)
repetitions = 10000
for dice_number in dice_numbers: # how many dice to sum
sum_container = []
for r in range(repetitions):
rool_sum = sum((random.randint(1,6) for _ in range(dice_number)))
sum_container.append(rool_sum)
plot_histogram(sum_container)
I want to create something like
for r in repetitions:
rools_generator = (random.randint(1,6) for _ in range(dice_number)
sum_generator = (sum(rools_generator) for _ in range(r))
but the second time I reuse rools_generator
it is consumed. Do I need to construct a generator class?