random number including 1 - i.e. [0,1]

AggieDan04 danb_83 at yahoo.com
Wed Jun 10 00:06:49 EDT 2009


On Jun 9, 4:33 pm, Esmail <ebo... at hotmail.com> wrote:
> Hi,
>
> random.random() will generate a random value in the range [0, 1).
>
> Is there an easy way to generate random values in the range [0, 1]?
> I.e., including 1?

You could do random.uniform(0, 1.0000000000000002).  Due to floating-
point rounding, there are TWO original values that would return 1.0:
0.99999999999999978 or 0.99999999999999989; this may give you more
1.0's than you expected, and that's not even considering that Python's
PRNG could be non-uniformly-distributed over the 53-bit fractions.  If
you're nit-picky enough to care about the less-than-winning-the-
lottery chance of getting the maximum random value in the first place,
this might be a problem.



More information about the Python-list mailing list