From: Dr J R Stockton on
In comp.lang.javascript message <k4s6pgid.fsf(a)gmail.com>, Sat, 17 Apr
2010 11:36:42, Lasse Reichstein Nielsen <lrn.unread(a)gmail.com> posted:
>Johannes Baagoe <baagoe(a)baagoe.com> writes:
>
>> Dr J R Stockton :
>
>>> It would at least be better than current Safari and Chrome.
>>
>> I'm not sure of that, because I have no idea of how they generate their
>> random numbers. That is precisely the problem - one does not know.
>
>Luckily both are Open Source:
>Safari:
> http://trac.webkit.org/browser/trunk/JavaScriptCore/runtime/WeakRandom.h
>Chrome:
> http://code.google.com/p/v8/source/browse/trunk/src/v8.cc#171
>
>(Just like Firefox:
> http://hg.mozilla.org/tracemonkey/file/95d633a94459/js/src/jsmath.cpp#l415
>)

The code indicated above for Safari does not look capable of giving what
I see in Safari 4.0.5 - though one really needs to see not just the PRNG
but exactly how the returned IEEE Double is obtained from it.

<http://www.trusteer.com/sites/default/files/Temporary_User_Tracking_in_
Major_Browsers.pdf>, PDF, 335kB or thereabouts, "Temporary user tracking
in major browsers and Cross-domain information leakage and attacks",
Amit Klein September-November 2008, has more information on code used
behind Math.random, including IE but not Opera.

I think IE uses a 48-bit Lehmer, unintelligently made to give 53 bits
(Firefox does that better).

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
From: Dr J R Stockton on
In comp.lang.javascript message <l8udnQ9Hlf8Z3VHWnZ2dnUVZ8ipi4p2d(a)gigane
ws.com>, Mon, 19 Apr 2010 06:37:40, Johannes Baagoe <baagoe(a)baagoe.com>
posted:
>Dr J R Stockton :
>> Johannes Baagoe :
>
>>> Try [http://baagoe.com/en/RandomMusings/javascript/time.xhtml] :)
>
>> That [...] does not account for the overheads. I get 267ms for a
>> million Math.random, but do not get how long is spent in the script
>> and how long within Math.random's internal code.
>
>Point taken, and your arguments plus a bit of experimenting have
>convinced me that there is no way I can meaningfully measure the
>speed of Math.random on my page. I have therefore removed that test.

If you restore Math.random, readers can see the difference in speed
between Math.random and the others. It might also be useful to time
code including both a typical or common random generator and the sort of
code that in a real application might also be in the loop.

Crudely, if the RNG takes only half of the loop time, its own speed is
only half as important - and in a scientific Monte Carlo calculation
(disregarding such as Buffon's Needle), for example, the RNG speed will
be of minor importance.

Even if (arbitrary figures) you can only shoe that Math.random takes
1 us +- 100%, that is clearly much faster than something measuring 10 us
+- 25%.


>As for the what Math.random should provide, merits of 53 vs 32 bits,
>64-bit Lehmer vs. Safari's and Chrome's present implementations,
>whether it matters that the first value repeats, etc, I think we just
>have to agree to disagree.

On that we seem to be definitely agreed - let the readers consider that
as settled. Also that your interest is centred on higher-quality
generators than are commonly used for Math.random, whereas mine is in
having a Math.random that agrees with what a reasonable optimist might
hope for on reading the Standard.


--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)