[ python-Bugs-1621367 ] random import works?

SourceForge.net noreply at sourceforge.net
Tue Dec 26 09:51:32 CET 2006


Bugs item #1621367, was opened at 2006-12-23 11:04
Message generated for change (Comment added) made by mark-roberts
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1621367&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Extension Modules
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Msword (msword)
Assigned to: Nobody/Anonymous (nobody)
Summary: random import works?

Initial Comment:
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 at gmail.com

Thanks

----------------------------------------------------------------------

Comment By: Mark Roberts (mark-roberts)
Date: 2006-12-26 02:51

Message:
Logged In: YES 
user_id=1591633
Originator: NO

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

----------------------------------------------------------------------

Comment By: Dennis Allison (dallison)
Date: 2006-12-23 12:32

Message:
Logged In: YES 
user_id=31903
Originator: NO

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]






----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1621367&group_id=5470


More information about the Python-bugs-list mailing list