I believe the problem is with your test framework and not with random.choice(). The library function random.choice( seq ) selects, using a uniform distribution, one item from the sequence at each call. By the law of large numbers, if you have K items in the sequence, each should be returned K/N times, on average, after N calls. You should expect deviations even for fairly large N. If you want to test the randomess, use a chi-square test to test against the hypothes of uniform random selection with replacement. Of course there are many other statistical properties which ought to be checked, for example, the distribution of runs.
Consider the program:
import random
dist = [0,0,0,0,0]
for i in range(100000):
j = random.choice([0,1,2,3,4])
dist[j] += 1
print dist
which prints the distribution observed for each choice. With
100000 tries you'd expect each one to appear (on average) 20000
time. Running it on my a three times gives:
[19839, 19871, 19996, 20035, 20259]
[20043, 19870, 20025, 20109, 19953]
[19947, 20033, 19970, 20111, 19939]
|