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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 patch class Random { | 83 patch class Random { |
84 | 84 |
85 /*patch*/ factory Random([int seed]) { | 85 /*patch*/ factory Random([int seed]) { |
86 var state = _Random._setupSeed((seed == null) ? _Random._nextSeed() : seed); | 86 var state = _Random._setupSeed((seed == null) ? _Random._nextSeed() : seed); |
87 // Crank a couple of times to distribute the seed bits a bit further. | 87 // Crank a couple of times to distribute the seed bits a bit further. |
88 return new _Random._withState(state).._nextState() | 88 return new _Random._withState(state).._nextState() |
89 .._nextState() | 89 .._nextState() |
90 .._nextState() | 90 .._nextState() |
91 .._nextState(); | 91 .._nextState(); |
92 } | 92 } |
| 93 |
| 94 /*patch*/ factory Random.secure() { |
| 95 return new _SecureRandom(); |
| 96 } |
93 } | 97 } |
94 | 98 |
95 | 99 |
96 class _Random implements Random { | 100 class _Random implements Random { |
97 // Internal state of the random number generator. | 101 // Internal state of the random number generator. |
98 final _state; | 102 final _state; |
99 static const kSTATE_LO = 0; | 103 static const _kSTATE_LO = 0; |
100 static const kSTATE_HI = 1; | 104 static const _kSTATE_HI = 1; // Unused in Dart code. |
101 | 105 |
102 _Random._withState(Uint32List this._state); | 106 _Random._withState(Uint32List this._state); |
103 | 107 |
104 // 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. |
105 // http://en.wikipedia.org/wiki/Multiply-with-carry | 109 // http://en.wikipedia.org/wiki/Multiply-with-carry |
106 // 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. |
107 | 111 |
108 // Implements: | 112 // Implements: |
109 // var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64; | 113 // var state = |
110 // _state[kSTATE_LO] = state & _MASK_32; | 114 // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & ((1 << 64) - 1); |
111 // _state[kSTATE_HI] = state >> 32; | 115 // _state[_kSTATE_LO] = state & ((1 << 32) - 1); |
| 116 // _state[_kSTATE_HI] = state >> 32; |
112 // This is a native to prevent 64-bit operations in Dart, which | 117 // This is a native to prevent 64-bit operations in Dart, which |
113 // fail with --throw_on_javascript_int_overflow. | 118 // fail with --throw_on_javascript_int_overflow. |
114 void _nextState() native "Random_nextState"; | 119 void _nextState() native "Random_nextState"; |
115 | 120 |
116 int nextInt(int max) { | 121 int nextInt(int max) { |
117 const limit = 0x3FFFFFFF; | 122 const limit = 0x3FFFFFFF; |
118 if (max <= 0 || ((max > limit) && (max > _POW2_32))) { | 123 if ((max <= 0) || ((max > limit) && (max > _POW2_32))) { |
119 throw new ArgumentError("max must be positive and < 2^32:" | 124 throw new RangeError.range(max, 1, _POW2_32, "max", |
120 " $max"); | 125 "Must be positive and <= 2^32"); |
121 } | 126 } |
122 if ((max & -max) == max) { | 127 if ((max & -max) == max) { |
123 // Fast case for powers of two. | 128 // Fast case for powers of two. |
124 _nextState(); | 129 _nextState(); |
125 return _state[kSTATE_LO] & (max - 1); | 130 return _state[_kSTATE_LO] & (max - 1); |
126 } | 131 } |
127 | 132 |
128 var rnd32; | 133 var rnd32; |
129 var result; | 134 var result; |
130 do { | 135 do { |
131 _nextState(); | 136 _nextState(); |
132 rnd32 = _state[kSTATE_LO]; | 137 rnd32 = _state[_kSTATE_LO]; |
133 result = rnd32 % max; | 138 result = rnd32 % max; |
134 } while ((rnd32 - result + max) >= _POW2_32); | 139 } while ((rnd32 - result + max) > _POW2_32); |
135 return result; | 140 return result; |
136 } | 141 } |
137 | 142 |
138 double nextDouble() { | 143 double nextDouble() { |
139 return ((nextInt(1 << 26) * _POW2_27_D) + nextInt(1 << 27)) / _POW2_53_D; | 144 return ((nextInt(1 << 26) * _POW2_27_D) + nextInt(1 << 27)) / _POW2_53_D; |
140 } | 145 } |
141 | 146 |
142 bool nextBool() { | 147 bool nextBool() { |
143 return nextInt(2) == 0; | 148 return nextInt(2) == 0; |
144 } | 149 } |
145 | 150 |
146 // Constants used by the algorithm or masking. | 151 // Constants used by the algorithm. |
147 static const _MASK_32 = (1 << 32) - 1; | |
148 static const _MASK_64 = (1 << 64) - 1; | |
149 static const _POW2_32 = 1 << 32; | 152 static const _POW2_32 = 1 << 32; |
150 static const _POW2_53_D = 1.0 * (1 << 53); | 153 static const _POW2_53_D = 1.0 * (1 << 53); |
151 static const _POW2_27_D = 1.0 * (1 << 27); | 154 static const _POW2_27_D = 1.0 * (1 << 27); |
152 | 155 |
153 static const _A = 0xffffda61; | 156 static const _A = 0xffffda61; |
154 | 157 |
155 // Use a singleton Random object to get a new seed if no seed was passed. | 158 // Use a singleton Random object to get a new seed if no seed was passed. |
156 static var _prng = new _Random._withState(_initialSeed()); | 159 static var _prng = new _Random._withState(_initialSeed()); |
157 | 160 |
158 // This is a native to prevent 64-bit operations in Dart, which | 161 // This is a native to prevent 64-bit operations in Dart, which |
159 // fail with --throw_on_javascript_int_overflow. | 162 // fail with --throw_on_javascript_int_overflow. |
160 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; | 163 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; |
161 // Get a seed from the VM's random number provider. | 164 // Get a seed from the VM's random number provider. |
162 static Uint32List _initialSeed() native "Random_initialSeed"; | 165 static Uint32List _initialSeed() native "Random_initialSeed"; |
163 | 166 |
164 static int _nextSeed() { | 167 static int _nextSeed() { |
165 // Trigger the PRNG once to change the internal state. | 168 // Trigger the PRNG once to change the internal state. |
166 _prng._nextState(); | 169 _prng._nextState(); |
167 return _prng._state[kSTATE_LO]; | 170 return _prng._state[_kSTATE_LO]; |
168 } | 171 } |
169 } | 172 } |
| 173 |
| 174 |
| 175 class _SecureRandom implements Random { |
| 176 _SecureRandom() { |
| 177 // Throw early in constructor if entropy source is not hooked up. |
| 178 _getBytes(1); |
| 179 } |
| 180 |
| 181 // Return count bytes of entropy as a positive integer; count <= 8. |
| 182 static int _getBytes(int count) native "SecureRandom_getBytes"; |
| 183 |
| 184 int nextInt(int max) { |
| 185 RangeError.checkValueInInterval( |
| 186 max, 1, _POW2_32, "max", "Must be positive and <= 2^32"); |
| 187 final byteCount = ((max - 1).bitLength + 7) >> 3; |
| 188 if (byteCount == 0) { |
| 189 return 0; // Not random if max == 1. |
| 190 } |
| 191 var rnd; |
| 192 var result; |
| 193 do { |
| 194 rnd = _getBytes(byteCount); |
| 195 result = rnd % max; |
| 196 } while ((rnd - result + max) > (1 << (byteCount << 3))); |
| 197 return result; |
| 198 } |
| 199 |
| 200 double nextDouble() { |
| 201 return (_getBytes(7) >> 3) / _POW2_53_D; |
| 202 } |
| 203 |
| 204 bool nextBool() { |
| 205 return _getBytes(1).isEven; |
| 206 } |
| 207 |
| 208 // Constants used by the algorithm. |
| 209 static const _POW2_32 = 1 << 32; |
| 210 static const _POW2_53_D = 1.0 * (1 << 53); |
| 211 } |
| 212 |
OLD | NEW |