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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 | 106 |
107 // Implements: | 107 // Implements: |
108 // var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64; | 108 // var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64; |
109 // _state[kSTATE_LO] = state & _MASK_32; | 109 // _state[kSTATE_LO] = state & _MASK_32; |
110 // _state[kSTATE_HI] = state >> 32; | 110 // _state[kSTATE_HI] = state >> 32; |
111 // This is a native to prevent 64-bit operations in Dart, which | 111 // This is a native to prevent 64-bit operations in Dart, which |
112 // fail with --throw_on_javascript_int_overflow. | 112 // fail with --throw_on_javascript_int_overflow. |
113 void _nextState() native "Random_nextState"; | 113 void _nextState() native "Random_nextState"; |
114 | 114 |
115 int nextInt(int max) { | 115 int nextInt(int max) { |
116 // TODO(srdjan): Remove the 'limit' check once optimizing comparison of | 116 // TODO(srdjan): Remove the 'limit' check once optimizing comparison of |
117 // Smi-s with Mint constants. | 117 // Smi-s with Mint constants. |
118 final limit = 0x3FFFFFFF; | 118 final limit = 0x3FFFFFFF; |
119 if (max <= 0 || ((max > limit) && (max > _POW2_32))) { | 119 if (max <= 0 || ((max > limit) && (max > _POW2_32))) { |
120 throw new ArgumentError("max must be positive and < 2^32:" | 120 throw new ArgumentError("max must be positive and < 2^32:" |
121 " $max"); | 121 " $max"); |
122 } | 122 } |
123 if ((max & -max) == max) { | 123 if ((max & -max) == max) { |
124 // Fast case for powers of two. | 124 // Fast case for powers of two. |
125 _nextState(); | 125 _nextState(); |
126 return _state[kSTATE_LO] & (max - 1); | 126 return _state[kSTATE_LO] & (max - 1); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 static int _nextSeed() { | 159 static int _nextSeed() { |
160 if (_prng == null) { | 160 if (_prng == null) { |
161 // TODO(iposva): Use system to get a random seed. | 161 // TODO(iposva): Use system to get a random seed. |
162 _prng = new Random(new DateTime.now().millisecondsSinceEpoch); | 162 _prng = new Random(new DateTime.now().millisecondsSinceEpoch); |
163 } | 163 } |
164 // Trigger the PRNG once to change the internal state. | 164 // Trigger the PRNG once to change the internal state. |
165 _prng._nextState(); | 165 _prng._nextState(); |
166 return _prng._state[kSTATE_LO]; | 166 return _prng._state[kSTATE_LO]; |
167 } | 167 } |
168 } | 168 } |
OLD | NEW |