- Do not rely on time to seed the default PRNG.

R=asiva@google.com

Review URL: https://codereview.chromium.org//200483003

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@33704 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
iposva@google.com 2014-03-14 17:30:05 +00:00
parent f99c83e54a
commit 214ecc0e2a
3 changed files with 12 additions and 5 deletions

View file

@ -178,4 +178,12 @@ DEFINE_NATIVE_ENTRY(Random_setupSeed, 2) {
return Object::null();
}
DEFINE_NATIVE_ENTRY(Random_initialSeed, 0) {
Random* rnd = isolate->random();
int64_t seed = rnd->NextUInt32();
seed |= (static_cast<int64_t>(rnd->NextUInt32())) << 32;
return Integer::New(seed);
}
} // namespace dart

View file

@ -154,13 +154,11 @@ class _Random implements Random {
static const _A = 0xffffda61;
// Use a singleton Random object to get a new seed if no seed was passed.
static var _prng = null;
static var _prng = new Random(_initialSeed());
static int _initialSeed() native "Random_initialSeed";
static int _nextSeed() {
if (_prng == null) {
// TODO(iposva): Use system to get a random seed.
_prng = new Random(new DateTime.now().millisecondsSinceEpoch);
}
// Trigger the PRNG once to change the internal state.
_prng._nextState();
return _prng._state[kSTATE_LO];

View file

@ -121,6 +121,7 @@ namespace dart {
V(Math_doublePow, 2) \
V(Random_nextState, 1) \
V(Random_setupSeed, 2) \
V(Random_initialSeed, 0) \
V(DateNatives_currentTimeMillis, 0) \
V(DateNatives_timeZoneName, 1) \
V(DateNatives_timeZoneOffsetInSeconds, 1) \