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

Unified Diff: lib/math/random.dart

Issue 10831324: Cleanup of math sources. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: lib/math/random.dart
diff --git a/lib/math/random.dart b/lib/math/random.dart
index d533cf629899bd011bb4cca8f7ac4bb084f53ff5..efc5678bc69dd711424f659ef01b32cda049a33e 100644
--- a/lib/math/random.dart
+++ b/lib/math/random.dart
@@ -8,102 +8,33 @@
* A random number generator. The default implementation supplies a stream of
* pseudo-random bits which is not suitable for cryptographic purposes.
*/
-interface Random default _Random {
- /**
+class Random {
+ /**
* Creates a random-number generator. The optional parameter [seed] is used
* to initialize the internal state of the generator. The implementation of
* the random stream can change between releases of the library.
*
* Implementation note: The default implementation uses up to 64-bits of seed.
*/
- Random([int seed]);
+ external factory Random([int seed]);
- /**
+ /**
* Generates a positive random integer uniformly distributed on the range
* from 0, inclusive, to [max], exclusive.
*
* Implementation note: The default implementation supports [max] values
* between 1 and ((1<<32) - 1) inclusive.
*/
- int nextInt(int max);
+ abstract int nextInt(int max);
- /**
+ /**
* Generates a positive random floating point value uniformly distributed on
* the range from 0.0, inclusive, to 1.0, exclusive.
*/
- double nextDouble();
+ abstract double nextDouble();
- /**
+ /**
* Generates a random boolean value.
*/
- bool nextBool();
-}
-
-class _Random implements Random {
-
- _Random([int seed = null]) {
- if (seed == null) {
- seed = _nextSeed();
- }
- do {
- seed = (seed + 0x5A17) & _MASK_64;
- } while (seed == 0);
- _state = seed;
- }
-
- // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32.
- // http://en.wikipedia.org/wiki/Multiply-with-carry
- // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1.
- int _nextInt32() {
- _state = ((_A * (_state & _MASK_32)) + (_state >> 32)) & _MASK_64;
- return _state & _MASK_32;
- }
-
- int nextInt(int max) {
- if (max <= 0 || max > _POW2_32) {
- throw new IllegalArgumentException("max must be positive and < 2^32:"
- " $max");
- }
- if ((max & -max) == max) {
- // Fast case for powers of two.
- return _nextInt32() & (max - 1);
- }
-
- var rnd32;
- var result;
- do {
- rnd32 = _nextInt32();
- result = rnd32 % max;
- } while (rnd32 - result + max >= _POW2_32);
- return result;
- }
-
- double nextDouble() {
- return ((nextInt(1 << (26)) << 27) + nextInt(1 << 27)) / _POW2_53_D;
- }
-
- bool nextBool() {
- return nextInt(1) == 0;
- }
-
- static final _MASK_32 = (1 << 32) - 1;
- static final _MASK_64 = (1 << 64) - 1;
- static final _POW2_32 = 1 << 32;
- static final _POW2_53_D = 1.0 * (1 << 53);
-
- static final _A = 0xffffda61;
-
- var _state;
-
- int _nextSeed() {
- if (_prng == null) {
- // TODO(iposva): Use system to get a random seed.
- _prng = new _Random(new Date.now().millisecondsSinceEpoch);
- }
- // Trigger the PRNG once to change the internal state.
- _prng._nextInt32();
- return _prng._state;
- }
-
- static var _prng = null;
+ abstract bool nextBool();
}
« lib/math/base.dart ('K') | « lib/math/math_sources.gypi ('k') | runtime/vm/vm.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698