random number including 1 - i.e. [0,1]
mensanator at aol.com
Wed Jun 10 19:21:14 CEST 2009
On Jun 10, 4:01 am, Mark Dickinson <dicki... at gmail.com> wrote:
> On Jun 10, 7:25 am, John Yeung <gallium.arsen... at gmail.com> wrote:
> > On Jun 10, 1:52 am, Steven D'Aprano
> > <ste... at REMOVE.THIS.cybersource.com.au> wrote:
> > > On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote:
> > > > 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.
> > > Which is?
> > That uniform(a, b) will return a random float in the semi-open
> > interval [a, b) for certain values of a and b; and in the closed
> > interval [a, b] for other values of a and b. (Swap a and b if a > b.)
> > To me, the fact that you sometimes get a semi-open interval and
> > sometimes a closed interval is worth noting in the docs.
> Do you want to submit a doc patch?
> For practical purposes, I think you'd be hard-pressed to find a
> test that could reliably distinguish between a large sample of values
> random.uniform(a, b) and a sample from a 'perfect' uniform
> distribution on
> the closed interval [a, b]. It's true that there are values of a and
> such that random.uniform(a, b) can never produce b.
So, the 2.6.2 documentation is STILL wrong. Before it implied
it was ALWAYS a semi-open interval, and now it says it's ALWAYS
a closed interval. But neither is correct.
I think a doc patch is definitely warranted.
> But for given a and b,
> not too close together, there may be many other values that can't be
> produced as well, so it hardly seems worth pointing out one particular
> value that can never be produced.
> Example: on a typical system there are almost 2**62 floats in the
> range [0, 1); the vast majority of these can never be produced by
> random.random(), which can only ever return one of 2**53 distinct
> (it essentially just returns a value of the form n/2**53 with n
> an integer in the range [0, 2**53)). So random.uniform(0, 1) will
> miss lots of possible values.
> On the other hand, it's easy to find examples of a and b such that
> random.uniform(a, b) has a significant chance of producing b.
> For example, take a = 10**16, b = 10**16 + 4, then there's about a 1
> in 4 chance of getting b. Or for a more extreme example, simply
> take a = b. Hence the doc change.
More information about the Python-list