In comp.lang.javascript message <rpWdnTScPMPX2d_XnZ2dnUVZ8kNi4p2d@gigane
Dr J R Stockton :
Lehmer's algorithm with a well-chosen multiplier was the method of
choice thirty years ago. Today, it is mainly used as a one-liner to
seed other methods with less statistical bias and whose output cannot
not be as easily predicted from a few known samples.
I'm not so sure about "mainly used"; you may be underestimating
the number who know no better.
Actually, IMHO, quite adequate for its intended uses: somewhat
unpredictable visual effects, games, recreational simulations, etc.
Agreed; nevertheless, since an IEEE Double has 53 bits of resolution,
and 64-bit Lehmer is sufficiently easy to implement, and other good,
easy methods are well enough known (if not by me), it seem inexcusable
to provide anything other than an equivalent to a random selection from
the 53-bit integers then divided by 2^53. Things should be as good as
easily possible, even if the necessity is not obvious.
In Safari 4, Math.random seems to be slightly worse, in at least one
respect, than that in Safari 3.1.1.
But neither can your function RRN1(). It is actually worse than most
Math.random implementations - 32 bits vs. 48.
It is an emulation of Borland's 32-bit one, in Pascal & Delphi (but not
necessarily the latest Delphi). That might be considered of very
adequate quality when introduced, 22 years ago or earlier; but, for
similar reasons, not now. Random in Delphi should return an Extended;
and all one need do for that is to take a good random Int64 and precede
it by a positive signed word constant for the appropriate coded
exponent.
Each value would occur exactly the same number of times in a cycle -
but so would a simple counter. Apart from that, I fail to see what
probability would be equal. For instance, the lowest-order bit would
alternate quite regularly between 0 and 1.
The stated conditions were intended to be necessary, but not to be
satisfactorily sufficient.
An IEEE Double with a regular alternating pattern in its LSB, while not
ideal, is considerably better that what three recent PC browsers
provide, which appear to be IEEE Doubles with 20 or more LSBs all zero.
I fail to see the distinction. Any vendor that sells a "commercial
grade" CSPRNG which are not "national security grade" is a swindler,
since there are plenty of public domain CSPRNGs that are as secure
as anyone, even the NSA or the GCHQ, may use or fear.
I disagree on both points, as above.
There are quite decent ECMASCript implementations of excellent hash
and encryption algorithms that can be used to make even "national
security grade" CSPRNGs by the standard methods using cryptographic
primitives. E.g.:
http://www.movable-type.co.uk/scripts/sha1.html
and
http://www.movable-type.co.uk/scripts/aes.html. 256 bit AES in
counter mode should really be as secure as anyone would wish, or fear.
(It is approved by the NSA even for TOP SECRET classified documents.)
The main problem is that it requires quite elaborate code.
It seems to me that your placement of the stools is rather arbitrary.
I would definitely set 2 much higher, since there are many PRNGs available
which much better statistical behaviour than Lehmer's algorithm, and
more entropy.
But there seems a risk in trying to persuade browser makers to provide
anything better than what I meant in 2; they might then prefer not to
bother.
On the other hand, even 256 bit AES does not, per se, address the
problem of seeding the generator in a suitably impredictable way.
[Seeds]
They can be set, as in my cited page, by passing in an Object
containing those numbers; then, by having several Objects, one can
have several random streams. There can be an Object checker, too.
Something like this?
<g> IMHO, needs comment : // 134217728 = 2^27 </g>
Something like. Passing in the Seed suffices; my idea would pass in
also the numbers within the function, so that one piece of code could
produce various types of Random.
function Lehmer(seed) {
var x = seed; return (lehmer27() / 134217728 + lehmer27()) / 134217728;
}
I was not sure whether I ought to be able to understand whether, if the
two calls return integers in that range that have no significant
correlation, that should give an IEEE Double with all the desirable
properties = until I realised the value of 2^27. In particular, a
result at least 0.5 must be a multiple of 2^-53, in an IEEE Double; but
a smaller one can perhaps be an odd multiple of 2^-54. It might be
better to use lehmer26 and lehmer27; or to mask out a bit.
My suggestion is to replace Lehmer, e.g., by Marsaglia's KISS32. It
is not much more complicated and passes much more stringent
statistical tests. It also is much less easily predictable from
observed output. Finally, its internal state has slightly more valid
values than there are valid RFC 4122 UUIDs, which makes it at least
as resistant to accidental collisions. Provided, that is, that one
finds a way to seed it with truly random values...
There should be provided an obvious way to seed it with known values
(Delphi : assign to RandSeed); possibly as setRandSeed(X). That moves
the worry into another routine, which the user can provide - somehow.
But it is not a practical solution for the World Wide Web. The challenge
there is to maintain a large enough entropy pool with only the information
available to the browser. I think it is possible - but not by looking
up RAND corporation's One Million Random Digits, or, indeed, on relying
on voluntary user input, which tends not to be random at all.
That depends on the application. Web pages do not necessarily return
results to the server; they can instead be shown at the client end to
the user. In that case, it's the user's responsibility to enter a seed
with enough entropy for the purpose.
I wonder whether the current RAND book contains the same 1,000,000
digits as the first edition - given the estimate that probably one of
the original digits is wrong?
Is there a small set of URLs that you can recommend to Garrett Smith for
FAQ section 5.5, <
http://jibbering.com/faq/index.html#randomNumber>,
ones leading to the world of good Random Number generation? FAQ
reference 1 is OK, 2 content is wrong, and you've seen 3 (which has some
such URLs).
IMHO, your thoughts should be known to those working on ECMA 262 Final
Draft 5
<
http://www.ecma-international.org/publications/files/drafts/
tc39-2009-025.pdf>
which includes an address for feedback "by July 15, 2009".