I want to generate 4 random integer numbers in the range [1,4]
in a uniform distribution. For example, each number appears 3 times for a sequence of 12 elements.
you can use a generator:
from random import randint
def getNum1To4(runs):
occurences = {n+1:0 for n in range(4)}
for i in range(runs):
options = [n for n in occurences if occurences[n] < runs / 4]
choice = options[randint(0, len(options) - 1)]
occurences[choice] += 1
yield choice
outputs:
>>> runs = 8
>>> gen = getNum1To4(8)
>>> for n in range(runs): print gen.next()
2
1
3
1
3
4
4
2
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments