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

Robert Kern robert.kern at gmail.com
Wed Jun 10 14:47:40 EDT 2009


On 2009-06-09 19:27, Mensanator wrote:
> On Jun 9, 6:12 pm, Robert Kern<robert.k... at gmail.com>  wrote:
>> On 2009-06-09 18:05, Mensanator wrote:
>>
>>
>>
>>
>>
>>> 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?
>>>> I am implementing an algorithm and want to stay as true to the
>>>> original design specifications as possible though I suppose the
>>>> difference between the two max values might be minimal.
>>>> Thanks,
>>>> Esmail
>>>> ps: I'm confused by the docs for uniform():
>>>> random.uniform(a, b)
>>>>        Return a random floating point number N such that a<= N<= b for a<= b
>>> That's wrong. Where did you get it?
>> http://docs.python.org/library/random
>
> Ok, but the 2.6.1 docs say
>
> random.uniform(a, b)
> Return a random floating point number N such that a<= N<  b
> for a<= b and b<= N<  a for b<  a.
>
> Is that a new feature of 2.6.2?

As already pointed out, it's not really a new feature of the method, but rather 
a fix for the buggy documentation. Because of floating point arithmetic, one 
cannot guarantee that a+(b-a)*u is strictly in [a,b) even though u is strictly 
in [0,1).

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list