mirror of
https://github.com/dart-lang/sdk
synced 2024-09-20 04:21:32 +00:00
- 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:
parent
57a60316f0
commit
624273f372
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue