<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>36673</bug_id>
          
          <creation_ts>2010-03-26 13:47:17 -0700</creation_ts>
          <short_desc>Math.random repeats values in different workers</short_desc>
          <delta_ts>2017-03-19 13:51:03 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>PC</rep_platform>
          <op_sys>OS X 10.6</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Ben Shapiro">webkit</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ap</cc>
    
    <cc>bfulgham</cc>
    
    <cc>cben</cc>
    
    <cc>jiewen_tan</cc>
    
    <cc>oliver</cc>
    
    <cc>webkit</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>204767</commentid>
    <comment_count>0</comment_count>
    <who name="Ben Shapiro">webkit</who>
    <bug_when>2010-03-26 13:47:17 -0700</bug_when>
    <thetext>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&apos;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</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>204887</commentid>
    <comment_count>1</comment_count>
    <who name="Oliver Hunt">oliver</who>
    <bug_when>2010-03-26 18:11:15 -0700</bug_when>
    <thetext>We seed the random number generator with WTF::currentTime at JSGlobalData.cpp:146 -- theoretically if we&apos;re spinning the new contexts up fast enough they&apos;ll all have the same seed.  Should we seed with randomNumber() instead?

I haven&apos;t tested to confirm this myself so this is simply theorising...</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>204904</commentid>
    <comment_count>2</comment_count>
    <who name="Ben Shapiro">webkit</who>
    <bug_when>2010-03-26 18:51:09 -0700</bug_when>
    <thetext>What&apos;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.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>248710</commentid>
    <comment_count>3</comment_count>
    <who name="Oliver Hunt">oliver</who>
    <bug_when>2010-07-08 21:24:36 -0700</bug_when>
    <thetext>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.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>922831</commentid>
    <comment_count>4</comment_count>
    <who name="Beni Paskin-Cherniavsky">cben</who>
    <bug_when>2013-08-30 00:12:06 -0700</bug_when>
    <thetext>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-&gt;lexicalGlobalObject()-&gt;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 &amp; process id.
But what it initializes are rand()/random() which are not used in any way for Math.random() AFAICT.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>