Chromium Code Reviews| 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) => MathNatives.pow(x, exponent); | 6 patch num pow(num x, num exponent) => MathNatives.pow(x, exponent); |
| 7 patch double atan2(num a, num b) => MathNatives.atan2(a, b); | 7 patch double atan2(num a, num b) => MathNatives.atan2(a, b); |
| 8 patch double sin(num x) => MathNatives.sin(x); | 8 patch double sin(num x) => MathNatives.sin(x); |
| 9 patch double cos(num x) => MathNatives.cos(x); | 9 patch double cos(num x) => MathNatives.cos(x); |
| 10 patch double tan(num x) => MathNatives.tan(x); | 10 patch double tan(num x) => MathNatives.tan(x); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 | 61 |
| 62 var rnd32; | 62 var rnd32; |
| 63 var result; | 63 var result; |
| 64 do { | 64 do { |
| 65 rnd32 = _nextInt32(); | 65 rnd32 = _nextInt32(); |
| 66 result = rnd32 % max; | 66 result = rnd32 % max; |
| 67 } while ((rnd32 - result + max) >= _POW2_32); | 67 } while ((rnd32 - result + max) >= _POW2_32); |
| 68 return result; | 68 return result; |
| 69 } | 69 } |
| 70 | 70 |
| 71 int nextIntRange(int min, int max) { | |
| 72 int range = max - min; | |
| 73 if (range <= 0) { | |
|
floitsch
2013/11/18 13:36:03
Doesn't test if max or min is negative.
| |
| 74 throw new ArgumentError("min is greater then or equal to max"); | |
|
floitsch
2013/11/18 13:36:03
Would be nice to have the values in the error mess
| |
| 75 } | |
| 76 range = nextInt(range); | |
|
floitsch
2013/11/18 13:36:03
I wouldn't have called the result of the random-ca
| |
| 77 return min + range; | |
| 78 } | |
| 79 | |
| 71 double nextDouble() { | 80 double nextDouble() { |
| 72 return ((nextInt(1 << 26) << 27) + nextInt(1 << 27)) / _POW2_53_D; | 81 return ((nextInt(1 << 26) << 27) + nextInt(1 << 27)) / _POW2_53_D; |
| 73 } | 82 } |
| 74 | 83 |
| 75 bool nextBool() { | 84 bool nextBool() { |
| 76 return nextInt(2) == 0; | 85 return nextInt(2) == 0; |
| 77 } | 86 } |
| 78 | 87 |
| 79 // Constants used by the algorithm or masking. | 88 // Constants used by the algorithm or masking. |
| 80 static const _MASK_32 = (1 << 32) - 1; | 89 static const _MASK_32 = (1 << 32) - 1; |
| 81 static const _MASK_64 = (1 << 64) - 1; | 90 static const _MASK_64 = (1 << 64) - 1; |
| 82 static const _POW2_32 = 1 << 32; | 91 static const _POW2_32 = 1 << 32; |
| 83 static const _POW2_53_D = 1.0 * (1 << 53); | 92 static const _POW2_53_D = 1.0 * (1 << 53); |
| 84 | 93 |
| 85 static const _A = 0xffffda61; | 94 static const _A = 0xffffda61; |
| 86 | 95 |
| 87 // Use a singleton Random object to get a new seed if no seed was passed. | 96 // Use a singleton Random object to get a new seed if no seed was passed. |
| 88 static var _prng = null; | 97 static var _prng = null; |
| 89 | 98 |
| 90 static int _nextSeed() { | 99 static int _nextSeed() { |
| 91 if (_prng == null) { | 100 if (_prng == null) { |
| 92 // TODO(iposva): Use system to get a random seed. | 101 // TODO(iposva): Use system to get a random seed. |
| 93 _prng = new Random(new Date.now().millisecondsSinceEpoch); | 102 _prng = new Random(new Date.now().millisecondsSinceEpoch); |
| 94 } | 103 } |
| 95 // Trigger the PRNG once to change the internal state. | 104 // Trigger the PRNG once to change the internal state. |
| 96 return _prng._nextInt32(); | 105 return _prng._nextInt32(); |
| 97 } | 106 } |
| 98 } | 107 } |
| OLD | NEW |