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

John Yeung gallium.arsenide at gmail.com
Wed Jun 10 01:21:26 EDT 2009


On Jun 9, 11:24 pm, Steven D'Aprano
<ste... at REMOVE.THIS.cybersource.com.au> wrote:
> On Tue, 09 Jun 2009 18:28:23 -0700, John Yeung wrote:
> > The docs are now... sort of correct.  For some values of a and b,
> > uniform() can never return b.  Notably, I believe uniform(0, 1) is
> > equivalent to random(), and will never return 1.  However, uniform(1, 2)
> > CAN return 2, if this is any indication:
>
> >>>> a=0.0
> >>>> b=1.0
> >>>> a+(b-a)*z < b
> > True
> >>>> a=1.0
> >>>> b=2.0
> >>>> a+(b-a)*z < b
> > False
>
> But you haven't shown what value z has, so there's no way of interpreting
> that example.

I'm pretty aggressive about snipping.  I left off the quote of z from
Gabriel.  He chose z to be the largest value that random.random() can
return; namely, the largest float smaller than 1.  I've just carried
over that value into my example.

The point of my example is, with z < 1, uniform(0, 1) is always less
than 1, but with z < 1, uniform(1, 2) can be 2, according to Gabriel's
description of uniform().

Therefore, to me the most up-to-date docs (which say that uniform(a,
b) returns a float in the closed interval [a, b]) is closer to correct
than before, but still fails to point out the full subtlety of the
behavior.

John



More information about the Python-list mailing list