Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does this truely generate a random foating point number? (Python)

Tags:

python

random

For an introduction to Python course, I'm looking at generating a random floating point number in Python, and I have seen a standard recommended code of

import random

lower = 5
upper = 10
range_width = upper - lower
x = random.random() * range_width + lower

for a random floating point from 5 up to but not including 10.

It seems to me that the same effect could be achieved by:

import random

x = random.randrange(5, 10) + random.random()

Since that would give an integer of 5, 6, 7, 8, or 9, and then tack on a decimal to it.

The question I have is would this second code still give a fully even probability distribution, or would it not keep the full randomness of the first version?

like image 942
user6052205 Avatar asked Mar 17 '17 21:03

user6052205


People also ask

How do you generate a random floating-point number in Python?

Use a random. random() function of a random module to generate a random float number uniformly in the semi-open range [0.0, 1.0) . Note: A random() function can only provide float numbers between 0.1. to 1.0. Us uniform() method to generate a random float number between any two numbers.

Are Python random numbers truly random?

Most random data generated with Python is not fully random in the scientific sense of the word. Rather, it is pseudorandom: generated with a pseudorandom number generator (PRNG), which is essentially any algorithm for generating seemingly random but still reproducible data.

How does Python random generate random numbers?

For "secure" random numbers, Python doesn't actually generate them: it gets them from the operating system, which has a special driver that gathers entropy from various real-world sources, such as variations in timing between keystrokes and disk seeks.

Is NP random actually random?

Indeed, whenever we call a python function, such as np. random. rand() the output can only be deterministic and cannot be truly random. Hence, numpy has to come up with a trick to generate sequences of numbers that look like random and behave as if they came from a purely random source, and this is what PRNG are.


2 Answers

According to the documentation then yes random() is indeed a uniform distribution.

random(), which generates a random float uniformly in the semi-open range [0.0, 1.0). Python uses the Mersenne Twister as the core generator.

So both code examples should be fine. To shorten your code, you can equally do:

random.uniform(5, 10)

Note that uniform(a, b) is simply a + (b - a) * random() so the same as your first example.

The second example depends on the version of Python you're using. Prior to 3.2 randrange() could produce a slightly uneven distributions.

like image 153
vallentin Avatar answered Nov 09 '22 22:11

vallentin


There is a difference. Your second method is theoretically superior, although in practice it only matters for large ranges. Indeed, both methods will give you a uniform distribution. But only the second method can return all values in the range that are representable as a floating point number.

Since your range is so small, there is no appreciable difference. But still there is a difference, which you can see by considering a larger range. If you take a random real number between 0 and 1, you get a floating-point representation with a given number of bits. Now suppose your range is, say, in the order of 2**32. By multiplying the original random number by this range, you lose 32 bits of precision in the result. Put differently, there will be gaps between the values that this method can return. The gaps are still there when you multiply by 4: You have lost the two least significant bits of the original random number.

like image 43
alexis Avatar answered Nov 09 '22 20:11

alexis