vendredi 17 mars 2017

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

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?




Aucun commentaire:

Enregistrer un commentaire