dimanche 11 février 2018

Why does random.random() take up two random.randint() values?

I've developed a simple application that generates test data series and I've built it to be able to be repeatable, using a random seed. I noticed the following and wanted to know why this happens:

>>> random.seed(1)
>>> [random.randint(0,10) for _ in range(0,10)]
[2, 9, 1, 4, 1, 7, 7, 7, 10, 6]
>>> random.seed(1)
>>> random.random()
0.13436424411240122
>>> [random.randint(0,10) for _ in range(0,10)]
[1, 4, 1, 7, 7, 7, 10, 6, 3, 1]

Note how a single call to random() uses up two values for randint(). I'm guessing this has something to do with the amount of random information required to generate a float vs. an int in the given range, but is there some way of keeping track of 'how many random values have been used so far?', i.e. how far along in the sequence of semi-random values the system is?

I ended up writing my own function, always using a single call to random.random() in its logic. So I'm not asking for a solution, just some background / an explanation.




Aucun commentaire:

Enregistrer un commentaire