OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // A part of the dart:math library. | |
6 | |
7 /** | |
8 * A random number generator. The default implementation supplies a stream of | |
9 * pseudo-random bits which is not suitable for cryptographic purposes. | |
10 * | |
11 * The basic function nextInt([int max]) generates a random integer in the | |
12 * semi-open range [0, max). | |
jjb
2012/05/15 20:19:17
I'd say this comment (about nextInt) doesn't belon
Ivan Posva
2012/05/16 00:13:20
I'll rely on you cleaning up the comments in anoth
| |
13 */ | |
14 interface Random default _Random { | |
15 /** | |
16 * Creates a random-number generator. The optional parameter [seed] is used | |
17 * to initialize the internal state of the generator. The implementation of | |
18 * the random stream can change between releases of the library. | |
jjb
2012/05/15 20:19:17
Each implementation (including the default) should
Ivan Posva
2012/05/16 00:13:20
Done.
| |
19 */ | |
20 Random([int seed]); | |
21 | |
22 // TODO(iposva): Do we really need this? | |
23 /** | |
24 * Creates a random-number generator which does not change between releases. | |
25 * This random-number generator is potentially not as fast as the default | |
26 * implementation, but it allows creating repeatable random-number sequences. | |
27 */ | |
jjb
2012/05/15 20:19:17
Might want to say "We anticipate that this constru
Ivan Posva
2012/05/16 00:13:20
Done.
| |
28 Random.stable([int seed]); | |
29 | |
30 /** | |
31 * Generates a random integer in the semi-open range [0, max). The default | |
jjb
2012/05/15 20:19:17
The phrase should be "half-open" rather than "semi
Ivan Posva
2012/05/16 00:13:20
Done.
| |
32 * implementation uses a [max] of ((1<<32) - 1) if no [max] value is supplied. | |
jjb
2012/05/15 20:19:17
I would say "2<sup>32</sup> - 1" rather than "((1<
| |
33 */ | |
34 int nextInt([int max]); | |
35 double nextDouble(); | |
jjb
2012/05/15 20:19:17
Presumably you want to document nextDouble and nex
| |
36 bool nextBool(); | |
37 } | |
38 | |
39 class _Random implements Random { | |
40 | |
41 _Random([int seed = null]) { | |
42 if (seed == null) { | |
43 // TODO(iposva): Use system to get a random seed. | |
44 seed = new Date.now().value; | |
jjb
2012/05/15 20:19:17
I would save the last seed used in a static variab
Ivan Posva
2012/05/16 00:13:20
Fixed as we had discussed previously. Use a per-is
| |
45 } | |
46 do { | |
47 seed = (seed + 0x5A17) & _MASK_32; | |
48 } while (seed == 0); | |
49 _seed = seed; | |
50 } | |
51 | |
52 factory _Random.stable([int seed = null]) { | |
53 if (seed == null) { | |
54 // If no seed is set use a predefined value. | |
55 seed = 103055; | |
56 } | |
57 return new _Random(seed); | |
58 } | |
59 | |
60 int nextInt([int max = null]) { | |
61 _seed = ((_A * (_seed & _MASK_32)) + (_seed >> 32)) & _MASK_64; | |
jjb
2012/05/15 20:19:17
Please put a reference (URL, literature, or whatev
| |
62 // TODO(iposva): Fix handling of max. | |
63 return (max == null) ? (_seed & _MASK_32) : ((_seed % max) & _MASK_32); | |
jjb
2012/05/15 20:19:17
Why are you doing the & after the %? And shouldn't
Ivan Posva
2012/05/16 00:13:20
Done.
| |
64 } | |
65 | |
66 double nextDouble() { | |
67 return nextInt() / _MASK_32_M1; | |
jjb
2012/05/15 20:19:17
Use 53 bits instead of 32 (the precision of the fl
Ivan Posva
2012/05/16 00:13:20
Done.
| |
68 } | |
69 | |
70 bool nextBool() { | |
71 return (nextInt() & 1) == 0; | |
72 } | |
73 | |
74 static final _MASK_32 = 0xffffffff; | |
75 static final _MASK_32_M1 = _MASK_32 - 1; | |
76 static final _MASK_64 = 0xffffffffffffffff; | |
77 static final _A = 0xffffda61; | |
78 var _seed; | |
79 } | |
OLD | NEW |