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

Esmail ebonak at hotmail.com
Wed Jun 10 01:53:22 CEST 2009


Gabriel Genellina wrote:
> En Tue, 09 Jun 2009 18:33:39 -0300, Esmail <ebonak at hotmail.com> escribió:
> 
>> 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?
> 
> I think you shouldn't worry about that - the difference may be as small 
> as 2**-53, or 0.0000000000000001

Ok, that's what I thought ...


> random() guarantees a semi-open interval (could return 0, but never 1). 
> But once you start to operate with the numbers, the limits become fuzzy.
> 
> a<b & n>0 => n.a<n.b
> 
> The above holds for real numbers but not always for floating point 
> arithmetic, so one cannot guarantee the semi-open interval anymore:
> 
> py> a=10.0
> py> b=11.0
> py> z = 0.9999999999999999  # assume random.random returned this
> py> z<1
> True
> py> a+(b-a)*z < b # the expression used for uniform(a,b)
> False
> py> a+(b-a)*z
> 11.0
> 
> The docs are already updated to reflect this:
> http://svn.python.org/view/python/trunk/Doc/library/random.rst?r1=68724&r2=68723&pathrev=68724 

Thanks for the information Gabriel,
Esmail




More information about the Python-list mailing list