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 15 matching lines...) Expand all Loading... | |
| 26 do { | 26 do { |
| 27 seed = (seed + 0x5A17) & _Random._MASK_64; | 27 seed = (seed + 0x5A17) & _Random._MASK_64; |
| 28 } while (seed == 0); | 28 } while (seed == 0); |
| 29 return new _Random._internal(seed); | 29 return new _Random._internal(seed); |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 | 33 |
| 34 class _Random implements Random { | 34 class _Random implements Random { |
| 35 // Internal state of the random number generator. | 35 // Internal state of the random number generator. |
| 36 var _state; | 36 var _state_lo; |
| 37 var _state_hi; | |
| 37 | 38 |
| 38 _Random._internal(this._state); | 39 _Random._internal(state) |
|
srdjan
2012/10/30 00:16:58
you can type this int.
| |
| 40 : _state_lo = (state & _MASK_32), _state_hi = (state >> 32); | |
| 39 | 41 |
| 40 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. | 42 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. |
| 41 // http://en.wikipedia.org/wiki/Multiply-with-carry | 43 // http://en.wikipedia.org/wiki/Multiply-with-carry |
| 42 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1. | 44 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1. |
| 43 int _nextInt32() { | 45 int _nextInt32() { |
| 44 _state = ((_A * (_state & _MASK_32)) + (_state >> 32)) & _MASK_64; | 46 var state = ((_A * (_state_lo)) + _state_hi) & _MASK_64; |
| 45 return _state & _MASK_32; | 47 _state_lo = state & _MASK_32; |
| 48 _state_hi = state >> 32; | |
| 49 return _state_lo; | |
| 46 } | 50 } |
| 47 | 51 |
| 48 int nextInt(int max) { | 52 int nextInt(int max) { |
| 49 if (max <= 0 || max > _POW2_32) { | 53 if (max <= 0 || max > _POW2_32) { |
| 50 throw new ArgumentError("max must be positive and < 2^32:" | 54 throw new ArgumentError("max must be positive and < 2^32:" |
| 51 " $max"); | 55 " $max"); |
| 52 } | 56 } |
| 53 if ((max & -max) == max) { | 57 if ((max & -max) == max) { |
| 54 // Fast case for powers of two. | 58 // Fast case for powers of two. |
| 55 return _nextInt32() & (max - 1); | 59 return _nextInt32() & (max - 1); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 82 | 86 |
| 83 // Use a singleton Random object to get a new seed if no seed was passed. | 87 // Use a singleton Random object to get a new seed if no seed was passed. |
| 84 static var _prng = null; | 88 static var _prng = null; |
| 85 | 89 |
| 86 static int _nextSeed() { | 90 static int _nextSeed() { |
| 87 if (_prng == null) { | 91 if (_prng == null) { |
| 88 // TODO(iposva): Use system to get a random seed. | 92 // TODO(iposva): Use system to get a random seed. |
| 89 _prng = new Random(new Date.now().millisecondsSinceEpoch); | 93 _prng = new Random(new Date.now().millisecondsSinceEpoch); |
| 90 } | 94 } |
| 91 // Trigger the PRNG once to change the internal state. | 95 // Trigger the PRNG once to change the internal state. |
| 92 _prng._nextInt32(); | 96 return _prng._nextInt32(); |
| 93 return _prng._state; | |
| 94 } | 97 } |
| 95 } | 98 } |
| OLD | NEW |