Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: runtime/lib/math_patch.dart

Issue 10919323: - Fix the implementation of Random.nextBool() and add test. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/lib/math/coin_test.dart » ('j') | tests/lib/math/coin_test.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 if ((max & -max) == max) { 53 if ((max & -max) == max) {
54 // Fast case for powers of two. 54 // Fast case for powers of two.
55 return _nextInt32() & (max - 1); 55 return _nextInt32() & (max - 1);
56 } 56 }
57 57
58 var rnd32; 58 var rnd32;
59 var result; 59 var result;
60 do { 60 do {
61 rnd32 = _nextInt32(); 61 rnd32 = _nextInt32();
62 result = rnd32 % max; 62 result = rnd32 % max;
63 } while (rnd32 - result + max >= _POW2_32); 63 } while ((rnd32 - result + max) >= _POW2_32);
64 return result; 64 return result;
65 } 65 }
66 66
67 double nextDouble() { 67 double nextDouble() {
68 return ((nextInt(1 << (26)) << 27) + nextInt(1 << 27)) / _POW2_53_D; 68 return ((nextInt(1 << 26) << 27) + nextInt(1 << 27)) / _POW2_53_D;
69 } 69 }
70 70
71 bool nextBool() { 71 bool nextBool() {
72 return nextInt(1) == 0; 72 return nextInt(2) == 0;
Ivan Posva 2012/09/17 21:22:13 This is the only functional change!
73 } 73 }
74 74
75 // Constants used by the algorithm or masking. 75 // Constants used by the algorithm or masking.
76 static const _MASK_32 = (1 << 32) - 1; 76 static const _MASK_32 = (1 << 32) - 1;
77 static const _MASK_64 = (1 << 64) - 1; 77 static const _MASK_64 = (1 << 64) - 1;
78 static const _POW2_32 = 1 << 32; 78 static const _POW2_32 = 1 << 32;
79 static const _POW2_53_D = 1.0 * (1 << 53); 79 static const _POW2_53_D = 1.0 * (1 << 53);
80 80
81 static const _A = 0xffffda61; 81 static const _A = 0xffffda61;
82 82
83 // Use a singleton Random object to get a new seed if no seed was passed. 83 // Use a singleton Random object to get a new seed if no seed was passed.
84 static var _prng = null; 84 static var _prng = null;
85 85
86 static int _nextSeed() { 86 static int _nextSeed() {
87 if (_prng == null) { 87 if (_prng == null) {
88 // TODO(iposva): Use system to get a random seed. 88 // TODO(iposva): Use system to get a random seed.
89 _prng = new Random(new Date.now().millisecondsSinceEpoch); 89 _prng = new Random(new Date.now().millisecondsSinceEpoch);
90 } 90 }
91 // Trigger the PRNG once to change the internal state. 91 // Trigger the PRNG once to change the internal state.
92 _prng._nextInt32(); 92 _prng._nextInt32();
93 return _prng._state; 93 return _prng._state;
94 } 94 }
95 } 95 }
OLDNEW
« no previous file with comments | « no previous file | tests/lib/math/coin_test.dart » ('j') | tests/lib/math/coin_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698