Why does random.random () accept two random.randint () values?

I developed a simple application that generates a series of test data, and I built it to be able to be repeatable using random seed. I noticed the following and wanted to find out why this is happening:

>>> 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] 

Notice how one call to random () uses two values ​​for randint (). I suppose this has something to do with the amount of random information needed to create a float versus int in a given range, but is there a way to keep track of β€œhow many random values ​​have been used so far?”, I.e. how far in the sequence of semi-random values is the system equal to?

I ended up writing my own function, always using a single call to random.random () in my logic. So I do not ask permission, just a little background / explanation.

+5
source share
1 answer

Your guess is true, at least for the latest version of CPython. There are several relevant places in the code for a random module where you can understand why this is happening. I would look at all this implementation detail, but you can see that essentially groups of 32 random bits are generated as needed to get random numbers. For this reason, receiving 53 random bits (to represent the fractional part of the double) uses twice as many random bits than receiving 4 random bits.

Regarding the opportunity to talk about how much random data was generated, the provided functions do not seem to give you a simple and reliable way to do this.

0
source

Source: https://habr.com/ru/post/1275300/


All Articles