New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
random import works? #44375
Comments
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. Thanks |
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 [19839, 19871, 19996, 20035, 20259] |
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).
|
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: |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: