| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import "dart:typed_data"; | 5 import "dart:typed_data"; |
| 6 | 6 |
| 7 // A VM patch of the dart:math library. | 7 // A VM patch of the dart:math library. |
| 8 | 8 |
| 9 // If [x] is an [int] and [exponent] is a non-negative [int], the result is | 9 // If [x] is an [int] and [exponent] is a non-negative [int], the result is |
| 10 // an [int], otherwise the result is a [double]. | 10 // an [int], otherwise the result is a [double]. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 // TODO(iposva): Handle patch methods within a patch class correctly. | 42 // TODO(iposva): Handle patch methods within a patch class correctly. |
| 43 patch class Random { | 43 patch class Random { |
| 44 | 44 |
| 45 /*patch*/ factory Random([int seed]) { | 45 /*patch*/ factory Random([int seed]) { |
| 46 if (seed == null) { | 46 if (seed == null) { |
| 47 seed = _Random._nextSeed(); | 47 seed = _Random._nextSeed(); |
| 48 } | 48 } |
| 49 do { | 49 do { |
| 50 seed = (seed + 0x5A17) & _Random._MASK_64; | 50 seed = (seed + 0x5A17) & _Random._MASK_64; |
| 51 } while (seed == 0); | 51 } while (seed == 0); |
| 52 return new _Random._internal(seed); | 52 // Crank a couple of times to distribute the seed bits a bit further. |
| 53 return new _Random._internal(seed).._nextState() |
| 54 .._nextState() |
| 55 .._nextState() |
| 56 .._nextState(); |
| 53 } | 57 } |
| 54 } | 58 } |
| 55 | 59 |
| 56 | 60 |
| 57 class _Random implements Random { | 61 class _Random implements Random { |
| 58 // Internal state of the random number generator. | 62 // Internal state of the random number generator. |
| 59 final _state = new Uint32List(2); | 63 final _state = new Uint32List(2); |
| 60 static const kSTATE_LO = 0; | 64 static const kSTATE_LO = 0; |
| 61 static const kSTATE_HI = 1; | 65 static const kSTATE_HI = 1; |
| 62 | 66 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 static int _nextSeed() { | 124 static int _nextSeed() { |
| 121 if (_prng == null) { | 125 if (_prng == null) { |
| 122 // TODO(iposva): Use system to get a random seed. | 126 // TODO(iposva): Use system to get a random seed. |
| 123 _prng = new Random(new DateTime.now().millisecondsSinceEpoch); | 127 _prng = new Random(new DateTime.now().millisecondsSinceEpoch); |
| 124 } | 128 } |
| 125 // Trigger the PRNG once to change the internal state. | 129 // Trigger the PRNG once to change the internal state. |
| 126 _prng._nextState(); | 130 _prng._nextState(); |
| 127 return _prng._state[kSTATE_LO]; | 131 return _prng._state[kSTATE_LO]; |
| 128 } | 132 } |
| 129 } | 133 } |
| OLD | NEW |