Ignore:
Timestamp:
Nov 30, 2015, 7:39:59 PM (10 years ago)
Author:
[email protected]
Message:

Use a better RNG for Math.random()
https://bugs.webkit.org/show_bug.cgi?id=151641

Reviewed by Anders Carlsson.

Source/JavaScriptCore:

Updated for interface change.

  • runtime/JSGlobalObject.cpp:

(JSC::JSGlobalObject::setInputCursor):

Source/WTF:

Use 64 bits in the random number generator instead of 32 bit. (In
the end, JavaScript, which uses doubles, will only see 52 bits.) This
prevents programs that mulitply a random number by a large constant from
seeing non-random "banding" caused by zeroes in the low 20 bits.

I also took the opportunity to upgrade from GameRandom to Xorshift+,
since Xorshift+ passes more benchmarks for randomness, and is not any
slower or more complicated.

Now let us all remember the fateful words of Steve Weibe, who would be
King of Kong: "The randomness went the opposite way that it usually goes."

  • wtf/WeakRandom.h:

(WTF::WeakRandom::WeakRandom):
(WTF::WeakRandom::setSeed): Use standard naming.

(WTF::WeakRandom::seed): This function is safe now. "Unsafe" in function
names makes me itch.

(WTF::WeakRandom::get):
(WTF::WeakRandom::getUint32): Update to 64bit.

(WTF::WeakRandom::advance): The Xorshift+ algorithm.

(WTF::WeakRandom::initializeSeed): Deleted.
(WTF::WeakRandom::seedUnsafe): Deleted.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp

    r192751 r192855  
    10151015    // to avoid threading the input cursor through all the abstraction layers.
    10161016    if (cursor.isCapturing())
    1017         cursor.appendInput<SetRandomSeed>(m_weakRandom.seedUnsafe());
     1017        cursor.appendInput<SetRandomSeed>(m_weakRandom.seed());
    10181018    else if (cursor.isReplaying()) {
    10191019        if (SetRandomSeed* input = cursor.fetchInput<SetRandomSeed>())
    1020             m_weakRandom.initializeSeed(static_cast<unsigned>(input->randomSeed()));
     1020            m_weakRandom.setSeed(static_cast<unsigned>(input->randomSeed()));
    10211021    }
    10221022}
Note: See TracChangeset for help on using the changeset viewer.