Random is barely random at all?

前端 未结 11 537
失恋的感觉
失恋的感觉 2020-11-27 10:27

I did this to test the randomness of randint:

>>> from random import randint
>>>
>>> uniques = []
>>> for i in range(4500         


        
相关标签:
11条回答
  • 2020-11-27 10:33

    As an answer to the answer of Nimbuz:

    http://xkcd.com/221/

    alt text

    0 讨论(0)
  • 2020-11-27 10:35

    That's not a repeater. A repeater is when you repeat the same sequence. Not just one number.

    0 讨论(0)
  • 2020-11-27 10:35

    You are generating 4500 random numbers from a range 500 <= x <= 5000. You then check to see for each number whether it has been generated before. The birthday problem tells us what the probability is for two of those numbers to match given n tries out of a range d.

    You can also invert the formula to calculate how many numbers you have to generate until the chance of generating a duplicate is more than 50%. In this case you have a >50% chance of finding a duplicate number after 79 iterations.

    0 讨论(0)
  • 2020-11-27 10:42

    Before blaming Python, you should really brush up some probability & statistics theory. Start by reading about the birthday paradox

    By the way, the random module in Python uses the Mersenne twister PRNG, which is considered very good, has an enormous period and was extensively tested. So rest assured you're in good hands.

    0 讨论(0)
  • 2020-11-27 10:43

    True randomness definitely includes repetition of values before the whole set of possible values is exhausted. It would not be random otherwise, as you would be able to predict for how long a value would not be repeated.

    If you ever rolled dice, you surely got 3 sixes in row quite often...

    0 讨论(0)
  • 2020-11-27 10:47

    If you don't want repetative one, generate sequential array and use random.shuffle

    0 讨论(0)
提交回复
热议问题