Bug 36673 - Math.random repeats values in different workers
Summary: Math.random repeats values in different workers
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: PC OS X 10.6
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-03-26 13:47 PDT by Ben Shapiro
Modified: 2017-03-19 13:51 PDT (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ben Shapiro 2010-03-26 13:47:17 PDT
When calling Math.random() in different workers, Math.random() is returning the same values in the same order in each worker. 

For example, when executing the following code (in an object) in webkit in multiple workers, the object's redness is set to the same value in each worker:
	this.redness = Math.round(255*Math.random());

I do not have this problem in Chrome or in Firefox. 

If additional detail is required, the complete code is at the following URL:
http://github.com/bennytheshap/sabmit/raw/master/sabmit/public/javascripts/turtlestuff/turtle_base.js
Comment 1 Oliver Hunt 2010-03-26 18:11:15 PDT
We seed the random number generator with WTF::currentTime at JSGlobalData.cpp:146 -- theoretically if we're spinning the new contexts up fast enough they'll all have the same seed.  Should we seed with randomNumber() instead?

I haven't tested to confirm this myself so this is simply theorising...
Comment 2 Ben Shapiro 2010-03-26 18:51:09 PDT
What's odd is that it happens even when the workers are created with a large amount of time between them.

To see this in action, go here:
http://evil.getdown.org:3000/breeds/1

Click the Hatch! button. It behaves as expected on Firefox and Chrome.
Comment 3 Oliver Hunt 2010-07-08 21:24:36 PDT
Just discovered that when we switched to our new random number generator, we reintroduced seeding from the current time, if the new workers are all initialiser sufficiently quickly, the generator gets a shared seed.
Comment 4 Beni Paskin-Cherniavsky 2013-08-30 00:12:06 PDT
Is this still relevant?
I think your comments refer to
https://trac.webkit.org/changeset/50789/trunk/JavaScriptCore/runtime/JSGlobalData.cpp 
but JSGlobalData.cpp has been renamed VM.cpp since and no longer includes any seeding.
Seeding WeakRandom from time was fixed in Aug 2010 in https://trac.webkit.org/changeset/65947 .

What I see now:

Math.random()
 https://trac.webkit.org/browser/trunk/Source/JavaScriptCore/runtime/MathObject.cpp?rev=154868
calls exec->lexicalGlobalObject()->weakRandomNumber() implemented in
 https://trac.webkit.org/browser/trunk/Source/JavaScriptCore/runtime/JSGlobalObject.h?rev=154868#L490
as a shallow wrapper for
 https://trac.webkit.org/browser/trunk/Source/JavaScriptCore/runtime/WeakRandom.h

It is initialized in
 https://trac.webkit.org/browser/trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp?rev=154868#L138
from randomNumber():
 https://trac.webkit.org/browser/trunk/Source/WTF/wtf/RandomNumber.cpp?rev=154868
which is a shallow wrapper for cryptographicallyRandomNumber() which is a shared ARC4Random state:
 https://trac.webkit.org/browser/trunk/Source/WTF/wtf/CryptographicallyRandomNumber.cpp?rev=154868#L168
which initializes itself in ARC4RandomNumberGenerator::stir() from strong OS randomness:
 https://trac.webkit.org/browser/trunk/Source/WTF/wtf/OSRandomSource.h?rev=154868

Phew.

--

There is also
https://trac.webkit.org/browser/trunk/Source/WTF/wtf/RandomNumberSeed.h?rev=154868 
which *does* initialize on Unix from time & process id.
But what it initializes are rand()/random() which are not used in any way for Math.random() AFAICT.