- Avoid going into BigInteger domain for most of the Random state calculation.

- Makes it much easier to intrinsify the operation.
Review URL: https://codereview.chromium.org//11339027

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@14235 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
iposva@google.com 2012-10-30 00:20:13 +00:00
parent 57a60316f0
commit 624273f372

View file

@ -33,16 +33,20 @@ patch class Random {
class _Random implements Random {
// Internal state of the random number generator.
var _state;
var _state_lo;
var _state_hi;
_Random._internal(this._state);
_Random._internal(state)
: _state_lo = (state & _MASK_32), _state_hi = (state >> 32);
// The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32.
// http://en.wikipedia.org/wiki/Multiply-with-carry
// The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1.
int _nextInt32() {
_state = ((_A * (_state & _MASK_32)) + (_state >> 32)) & _MASK_64;
return _state & _MASK_32;
var state = ((_A * (_state_lo)) + _state_hi) & _MASK_64;
_state_lo = state & _MASK_32;
_state_hi = state >> 32;
return _state_lo;
}
int nextInt(int max) {
@ -89,7 +93,6 @@ class _Random implements Random {
_prng = new Random(new Date.now().millisecondsSinceEpoch);
}
// Trigger the PRNG once to change the internal state.
_prng._nextInt32();
return _prng._state;
return _prng._nextInt32();
}
}