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 // Patch file for dart:math library. | 5 // Patch file for dart:math library. |
6 | 6 |
7 patch double sqrt(num x) | 7 patch double sqrt(num x) |
8 => JS('double', r'Math.sqrt(#)', checkNum(x)); | 8 => JS('double', r'Math.sqrt(#)', checkNum(x)); |
9 | 9 |
10 patch double sin(num x) | 10 patch double sin(num x) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 class _Random implements Random { | 47 class _Random implements Random { |
48 // The Dart2JS implementation of Random doesn't use a seed. | 48 // The Dart2JS implementation of Random doesn't use a seed. |
49 const _Random(); | 49 const _Random(); |
50 | 50 |
51 int nextInt(int max) { | 51 int nextInt(int max) { |
52 if (max < 0) throw new ArgumentError("negative max: $max"); | 52 if (max < 0) throw new ArgumentError("negative max: $max"); |
53 if (max > 0xFFFFFFFF) max = 0xFFFFFFFF; | 53 if (max > 0xFFFFFFFF) max = 0xFFFFFFFF; |
54 return JS("int", "(Math.random() * #) >>> 0", max); | 54 return JS("int", "(Math.random() * #) >>> 0", max); |
55 } | 55 } |
56 | 56 |
57 int nextIntRange(int min, int max) { | |
58 if (max < 0) throw new ArgumentError("negative max: $max"); | |
59 if (min >= max) throw new ArgumentError("min is greater then or equal to max "); | |
floitsch
2013/11/18 13:36:03
80 chars. And the error message should contain the
| |
60 | |
61 int range = max - min; | |
62 range = nextInt(range); | |
floitsch
2013/11/18 13:36:03
Don't reuse the variable (which is not a range).
I
| |
63 return min + range; | |
64 } | |
65 | |
57 /** | 66 /** |
58 * Generates a positive random floating point value uniformly distributed on | 67 * Generates a positive random floating point value uniformly distributed on |
59 * the range from 0.0, inclusive, to 1.0, exclusive. | 68 * the range from 0.0, inclusive, to 1.0, exclusive. |
60 */ | 69 */ |
61 double nextDouble() => JS("double", "Math.random()"); | 70 double nextDouble() => JS("double", "Math.random()"); |
62 | 71 |
63 /** | 72 /** |
64 * Generates a random boolean value. | 73 * Generates a random boolean value. |
65 */ | 74 */ |
66 bool nextBool() => JS("bool", "Math.random() < 0.5"); | 75 bool nextBool() => JS("bool", "Math.random() < 0.5"); |
67 } | 76 } |
OLD | NEW |