John Yeung wrote:
On Jun 10, 1:52 am, Steven D'Aprano
<[email protected]> 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.
John
I took the following direct from "The Python Library Reference (Release
2.6.2)" , Guido van Rossum, Fred L. Drake, Jr. editor, June 10, 2009.
On p. 216,
Almost all module functions depend on the basic
function random(), which generates a random float uniformly
in the semi-open range [0.0, 1.0). Python uses the Mersenne Twister as
the core generator. It produces 53-bit
precision floats and has a period of 2**19937-1. The underlying
implementation in C is both fast and threadsafe.
The Mersenne Twister is one of the most extensively tested random
number generators in existence. However,
being completely deterministic, it is not suitable for all purposes,
and is completely unsuitable for cryptographic
purposes.
The notation above means that 0 is included but 1 is not (as pointed
out by Esmail). I agree with Esmail, that it is important to know if
this is correct, since the "drawing" of pseudo RVs from other
distributions can depend on this function.
The following is taken from MatLab (R2007b),
The rand function now supports
a method of random number generation called the Mersenne Twister. The
algorithm
used by this method, developed by Nishimura and Matsumoto, generates
double
precision values in the closed interval [2^(-53), 1-2^(-53)],
with a period of (2^19937-1)/2.
Note, that it will not generate a 0 or 1; i.e., the interval for the
pseudo RV can be written as (0,1) or [2^(-53), 1-2^(-53)], where the
latter is more informative.
For a full description of the Mersenne twister
algorithm, see http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html.
If indeed Python 2.6.2 is using the Mersenne twister algorithm as
defined by the creators of this algorithm (go to the link given above),
then
IMHO the documentation should be corrected.
I hope that this helps.
--V. Stokes
|