This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: random import works?
Type: Stage:
Components: Extension Modules Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: dallison, mark-roberts, msword
Priority: normal Keywords:

Created on 2006-12-23 17:04 by msword, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
random checker.py msword, 2006-12-23 17:04 Checks if random.choice() is all that random
Messages (4)
msg30861 - (view) Author: Msword (msword) Date: 2006-12-23 17:04
I'm just starting working with python, and it seems that random.choice() isn't all that random. After running the program(attached) a few times, each and every time it selects 1 < 2 < 3 < 4 < 5 and I can expect this to happen, meaning it isn't all that random.
Email: Kentsfx2@gmail.com

Thanks
msg30862 - (view) Author: Dennis Allison (dallison) Date: 2006-12-23 18:32
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]




msg30863 - (view) Author: Mark Roberts (mark-roberts) Date: 2006-12-26 08:51
This is an issue with the way you have written your initial application.  See the attachment that I've supplied to explain what is going wrong in your program.  It really boils down to you adding X to each value, while X is steadily increasing in value (1, 2, 3, 4, 5).  At one point, you actually add 1 to one value, and add 5 to another (for the same random choice).

- Mark
msg30864 - (view) Author: Mark Roberts (mark-roberts) Date: 2006-12-26 08:54
Sorry, it appears that I can't upload a file, or I'm missing the button.  Anyway, you can find the file you're lookign for here:

http://www.pandapocket.com/python/rand2.pys
History
Date User Action Args
2022-04-11 14:56:21adminsetgithub: 44375
2006-12-23 17:04:36mswordcreate