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

Robert Kern robert.kern at gmail.com
Wed Jun 10 16:32:05 EDT 2009

On 2009-06-10 14:46, Mark Dickinson wrote:
> On Jun 10, 8:15 pm, Robert Kern<robert.k... at gmail.com>  wrote:
>> On 2009-06-10 13:53, Terry Reedy wrote:
>>> A full technical discussion does not below in the docs, in my opinion. A
>>> wike article would be fine.
>> True. However, a brief note that "Due to floating point arithmetic, for some
>> values of a and b, b may or may not be one of the possible generated results."
>> might be worthwhile. The actual details of *why* this is the case can be
>> discussed elsewhere.
> I find it difficult to see how such a disclaimer would have any
> practical
> value, without also knowing *which* values of a and b are affected.
> It can certainly be useful to know that endpoints *aren't* included
> where
> that's true.  For example, knowing that random.random() can never
> produce the
> value 1.0 means that one can safely generate a mean 1 exponential
> variate with
> -log(1-random.random()), without worrying about the possibility of
> taking log
> of 0.
> But I don't know why it would be useful to know that endpoints *are*
> sometimes
> included, without knowing exactly when.

That's a fair point. However, one issue that hasn't been brought up is that it 
might be confusing to a user why random.random() returns values in a half-open 
interval while random.uniform() claims a closed interval. Even for reasonably 
sophisticated floating point users, it's not necessarily obvious that that is 
the reasoning behind the different claims.

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