random number including 1 - i.e. [0,1]
Mark Dickinson
dickinsm at gmail.com
Wed Jun 10 17:47:53 EDT 2009
Robert Kern <robert.kern at gmail.com> wrote:
> On 2009-06-10 15:54, Mark Dickinson wrote:
>> [...] I'm not sure I'm capable of coming up with extra wording
>> for the docs that won't just cause more confusion, so I'll leave that
>> to someone else.
>
> I did make a concrete suggestion.
Yes, you did. Thank you. Submitted as http://bugs.python.org/issue6261
>
>> I seem to recall that when this originally came up in the tracker
>> (issue 4979) the fun part of the analysis was proving that
>> random.uniform(a, b) can never produce values *outside* the interval
>> [a, b]. :-)
>
> I was a bit worried about that part myself for a little bit. :-)
>
I think the key was to show that multiplication by (1-2**-53) (the
largest possible output of random.random()) always makes any float
smaller in magnitude[*], so assuming that a <= b the value of the Python
expression random.random()*(b-a) can't be larger than the exact real
value of (b-a), which in turn means that a + random.random()*(b-a)
can't exceed b.
[*] Well, almost. This isn't true for subnormals. But if the result
of the subtraction b-a is subnormal then that subtraction was also
necessarily exact, so everything works in this case, too.
And of course I'm wrong. I shouldn't have said *never*, above:
>>> from random import uniform
>>> uniform(-1e308, 1e308)
inf
:-(
Somehow this doesn't seem worth either fixing or documenting, though.
--
Mark Dickinson
More information about the Python-list
mailing list