Random is barely random at all?

前端 未结 11 561
失恋的感觉
失恋的感觉 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:52

    There is the birthday paradox. Taking this into account you realize that what you are saying is that finding "764, 3875, 4290, 4378, 764" or something like that is not very random because a number in that sequence repeats. The true way to do it is to compare sequences to each other. I wrote a python script to do this.

    from random import randint
    y = 21533456
    uniques = []
    for i in range(y):  
        x1 = str(randint(500, 5000))
        x2 = str(randint(500, 5000))
        x3 = str(randint(500, 5000))
        x4 = str(randint(500, 5000))
        x = (x1 + ", " + x2 + ", " + x3 + ", " + x4)
    if x in uniques:
        raise Exception('We duped the sequence %d at iteration number %d' % (x, i))
    else:
        raise Exception('Couldn\'t find a repeating sequence in %d iterations' % (y))
    uniques.append(x)
    

提交回复
热议问题