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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 } | 92 } |
93 | 93 |
94 @patch factory Random.secure() { | 94 @patch factory Random.secure() { |
95 return new _SecureRandom(); | 95 return new _SecureRandom(); |
96 } | 96 } |
97 } | 97 } |
98 | 98 |
99 | 99 |
100 class _Random implements Random { | 100 class _Random implements Random { |
101 // Internal state of the random number generator. | 101 // Internal state of the random number generator. |
102 final _state; | 102 final Uint32List _state; |
103 static const _kSTATE_LO = 0; | 103 static const _kSTATE_LO = 0; |
104 static const _kSTATE_HI = 1; // Unused in Dart code. | 104 static const _kSTATE_HI = 1; // Unused in Dart code. |
105 | 105 |
106 _Random._withState(Uint32List this._state); | 106 _Random._withState(this._state); |
107 | 107 |
108 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. | 108 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. |
109 // http://en.wikipedia.org/wiki/Multiply-with-carry | 109 // http://en.wikipedia.org/wiki/Multiply-with-carry |
110 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1. | 110 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1. |
111 | 111 |
112 // Implements: | 112 // Implements: |
113 // var state = | 113 // var state = |
114 // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & ((1 << 64) - 1); | 114 // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & ((1 << 64) - 1); |
115 // _state[_kSTATE_LO] = state & ((1 << 32) - 1); | 115 // _state[_kSTATE_LO] = state & ((1 << 32) - 1); |
116 // _state[_kSTATE_HI] = state >> 32; | 116 // _state[_kSTATE_HI] = state >> 32; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 | 205 |
206 bool nextBool() { | 206 bool nextBool() { |
207 return _getBytes(1).isEven; | 207 return _getBytes(1).isEven; |
208 } | 208 } |
209 | 209 |
210 // Constants used by the algorithm. | 210 // Constants used by the algorithm. |
211 static const _POW2_32 = 1 << 32; | 211 static const _POW2_32 = 1 << 32; |
212 static const _POW2_53_D = 1.0 * (1 << 53); | 212 static const _POW2_53_D = 1.0 * (1 << 53); |
213 } | 213 } |
214 | 214 |
OLD | NEW |