| 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) { | 6 patch num pow(num x, num exponent) { |
| 7 if (exponent is int) { | 7 if (exponent is int) { |
| 8 return x.pow(exponent); | 8 return x.pow(exponent); |
| 9 } | 9 } |
| 10 // Double.pow will call exponent.toDouble(). | 10 // Double.pow will call exponent.toDouble(). |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 } | 49 } |
| 50 | 50 |
| 51 | 51 |
| 52 class _Random implements Random { | 52 class _Random implements Random { |
| 53 // Internal state of the random number generator. | 53 // Internal state of the random number generator. |
| 54 var _state; | 54 var _state; |
| 55 static const kSTATE_LO = 0; | 55 static const kSTATE_LO = 0; |
| 56 static const kSTATE_HI = 1; | 56 static const kSTATE_HI = 1; |
| 57 | 57 |
| 58 _Random._internal(state) { | 58 _Random._internal(state) { |
| 59 _state = new List(2); | 59 _state = new List.fixedLength(2); |
| 60 _state[kSTATE_LO] = state & _MASK_32; | 60 _state[kSTATE_LO] = state & _MASK_32; |
| 61 _state[kSTATE_HI] = state >> 32; | 61 _state[kSTATE_HI] = state >> 32; |
| 62 } | 62 } |
| 63 | 63 |
| 64 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. | 64 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. |
| 65 // http://en.wikipedia.org/wiki/Multiply-with-carry | 65 // http://en.wikipedia.org/wiki/Multiply-with-carry |
| 66 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1. | 66 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1. |
| 67 int _nextInt32() { | 67 int _nextInt32() { |
| 68 var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64; | 68 var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64; |
| 69 _state[kSTATE_LO] = state & _MASK_32; | 69 _state[kSTATE_LO] = state & _MASK_32; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 | 111 |
| 112 static int _nextSeed() { | 112 static int _nextSeed() { |
| 113 if (_prng == null) { | 113 if (_prng == null) { |
| 114 // TODO(iposva): Use system to get a random seed. | 114 // TODO(iposva): Use system to get a random seed. |
| 115 _prng = new Random(new Date.now().millisecondsSinceEpoch); | 115 _prng = new Random(new Date.now().millisecondsSinceEpoch); |
| 116 } | 116 } |
| 117 // Trigger the PRNG once to change the internal state. | 117 // Trigger the PRNG once to change the internal state. |
| 118 return _prng._nextInt32(); | 118 return _prng._nextInt32(); |
| 119 } | 119 } |
| 120 } | 120 } |
| OLD | NEW |