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

Side by Side 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: Another attempt 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « lib/math/math_sources.gypi ('k') | runtime/vm/vm.gypi » ('j') | no next file with comments »
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 part of the dart:math library. 5 // A part of the dart:math library.
6 6
7 /** 7 /**
8 * A random number generator. The default implementation supplies a stream of 8 * A random number generator. The default implementation supplies a stream of
9 * pseudo-random bits which is not suitable for cryptographic purposes. 9 * pseudo-random bits which is not suitable for cryptographic purposes.
10 */ 10 */
11 interface Random default _Random { 11 class Random {
12 /** 12 /**
13 * Creates a random-number generator. The optional parameter [seed] is used 13 * Creates a random-number generator. The optional parameter [seed] is used
14 * to initialize the internal state of the generator. The implementation of 14 * to initialize the internal state of the generator. The implementation of
15 * the random stream can change between releases of the library. 15 * the random stream can change between releases of the library.
16 * 16 *
17 * Implementation note: The default implementation uses up to 64-bits of seed. 17 * Implementation note: The default implementation uses up to 64-bits of seed.
18 */ 18 */
19 Random([int seed]); 19 external factory Random([int seed]);
20 20
21 /** 21 /**
22 * Generates a positive random integer uniformly distributed on the range 22 * Generates a positive random integer uniformly distributed on the range
23 * from 0, inclusive, to [max], exclusive. 23 * from 0, inclusive, to [max], exclusive.
24 * 24 *
25 * Implementation note: The default implementation supports [max] values 25 * Implementation note: The default implementation supports [max] values
26 * between 1 and ((1<<32) - 1) inclusive. 26 * between 1 and ((1<<32) - 1) inclusive.
27 */ 27 */
28 int nextInt(int max); 28 abstract int nextInt(int max);
29 29
30 /** 30 /**
31 * Generates a positive random floating point value uniformly distributed on 31 * Generates a positive random floating point value uniformly distributed on
32 * the range from 0.0, inclusive, to 1.0, exclusive. 32 * the range from 0.0, inclusive, to 1.0, exclusive.
33 */ 33 */
34 double nextDouble(); 34 abstract double nextDouble();
35 35
36 /** 36 /**
37 * Generates a random boolean value. 37 * Generates a random boolean value.
38 */ 38 */
39 bool nextBool(); 39 abstract bool nextBool();
40 } 40 }
41
42 class _Random implements Random {
43
44 _Random([int seed = null]) {
45 if (seed == null) {
46 seed = _nextSeed();
47 }
48 do {
49 seed = (seed + 0x5A17) & _MASK_64;
50 } while (seed == 0);
51 _state = seed;
52 }
53
54 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32.
55 // http://en.wikipedia.org/wiki/Multiply-with-carry
56 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1.
57 int _nextInt32() {
58 _state = ((_A * (_state & _MASK_32)) + (_state >> 32)) & _MASK_64;
59 return _state & _MASK_32;
60 }
61
62 int nextInt(int max) {
63 if (max <= 0 || max > _POW2_32) {
64 throw new IllegalArgumentException("max must be positive and < 2^32:"
65 " $max");
66 }
67 if ((max & -max) == max) {
68 // Fast case for powers of two.
69 return _nextInt32() & (max - 1);
70 }
71
72 var rnd32;
73 var result;
74 do {
75 rnd32 = _nextInt32();
76 result = rnd32 % max;
77 } while (rnd32 - result + max >= _POW2_32);
78 return result;
79 }
80
81 double nextDouble() {
82 return ((nextInt(1 << (26)) << 27) + nextInt(1 << 27)) / _POW2_53_D;
83 }
84
85 bool nextBool() {
86 return nextInt(1) == 0;
87 }
88
89 static final _MASK_32 = (1 << 32) - 1;
90 static final _MASK_64 = (1 << 64) - 1;
91 static final _POW2_32 = 1 << 32;
92 static final _POW2_53_D = 1.0 * (1 << 53);
93
94 static final _A = 0xffffda61;
95
96 var _state;
97
98 int _nextSeed() {
99 if (_prng == null) {
100 // TODO(iposva): Use system to get a random seed.
101 _prng = new _Random(new Date.now().millisecondsSinceEpoch);
102 }
103 // Trigger the PRNG once to change the internal state.
104 _prng._nextInt32();
105 return _prng._state;
106 }
107
108 static var _prng = null;
109 }
OLDNEW
« no previous file with comments | « 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