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 // A VM patch of the dart:math library. | 5 // A VM patch of the dart:math library. |
6 patch num pow(num x, num exponent) { | 6 patch num pow(num x, num exponent) { |
7 if (exponent is int) { | 7 if (exponent is int) { |
8 return x.pow(exponent); | 8 return x.pow(exponent); |
9 } | 9 } |
10 // Double.pow will call exponent.toDouble(). | 10 // Double.pow will call exponent.toDouble(). |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 do { | 44 do { |
45 seed = (seed + 0x5A17) & _Random._MASK_64; | 45 seed = (seed + 0x5A17) & _Random._MASK_64; |
46 } while (seed == 0); | 46 } while (seed == 0); |
47 return new _Random._internal(seed); | 47 return new _Random._internal(seed); |
48 } | 48 } |
49 } | 49 } |
50 | 50 |
51 | 51 |
52 class _Random implements Random { | 52 class _Random implements Random { |
53 // Internal state of the random number generator. | 53 // Internal state of the random number generator. |
54 final _state = new List(2); | 54 final _state = new List.fixedLength(2); |
55 static const kSTATE_LO = 0; | 55 static const kSTATE_LO = 0; |
56 static const kSTATE_HI = 1; | 56 static const kSTATE_HI = 1; |
57 | 57 |
58 _Random._internal(state) { | 58 _Random._internal(state) { |
59 _state[kSTATE_LO] = state & _MASK_32; | 59 _state[kSTATE_LO] = state & _MASK_32; |
60 _state[kSTATE_HI] = state >> 32; | 60 _state[kSTATE_HI] = state >> 32; |
61 } | 61 } |
62 | 62 |
63 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. | 63 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. |
64 // http://en.wikipedia.org/wiki/Multiply-with-carry | 64 // http://en.wikipedia.org/wiki/Multiply-with-carry |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 110 |
111 static int _nextSeed() { | 111 static int _nextSeed() { |
112 if (_prng == null) { | 112 if (_prng == null) { |
113 // TODO(iposva): Use system to get a random seed. | 113 // TODO(iposva): Use system to get a random seed. |
114 _prng = new Random(new Date.now().millisecondsSinceEpoch); | 114 _prng = new Random(new Date.now().millisecondsSinceEpoch); |
115 } | 115 } |
116 // Trigger the PRNG once to change the internal state. | 116 // Trigger the PRNG once to change the internal state. |
117 return _prng._nextInt32(); | 117 return _prng._nextInt32(); |
118 } | 118 } |
119 } | 119 } |
OLD | NEW |