OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Test that rnd.nextInt with a seed generates the same sequence each time. | 5 // Test that rnd.nextInt with a seed generates the same sequence each time. |
6 | 6 |
7 // Library tag to allow Dartium to run the test. | 7 // Library tag to allow Dartium to run the test. |
8 library random_test; | 8 library random_test; |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 import 'dart:math'; | 11 import 'dart:math'; |
12 | 12 |
13 main() { | 13 main() { |
14 checkSequence(); | 14 checkSequence(); |
15 checkSeed(); | 15 checkSeed(); |
16 } | 16 } |
17 | 17 |
18 void checkSequence() { | 18 void checkSequence() { |
19 // Check the sequence of numbers generated by the random generator for a seed | 19 // Check the sequence of numbers generated by the random generator for a seed |
20 // doesn't change unintendedly, and it agrees between implementations. | 20 // doesn't change unintendedly, and it agrees between implementations. |
21 var rnd = new Random(20130307); | 21 var rnd = new Random(20130307); |
22 // Make sure we do not break the random number generation. | 22 // Make sure we do not break the random number generation. |
23 // If the random algorithm changes, make sure both the VM and dart2js | 23 // If the random algorithm changes, make sure both the VM and dart2js |
24 // generate the same new sequence. | 24 // generate the same new sequence. |
25 var i = 1; | 25 var i = 1; |
26 Expect.equals( 0, rnd.nextInt(i *= 2)); | 26 Expect.equals(0, rnd.nextInt(i *= 2)); |
27 Expect.equals( 3, rnd.nextInt(i *= 2)); | 27 Expect.equals(3, rnd.nextInt(i *= 2)); |
28 Expect.equals( 7, rnd.nextInt(i *= 2)); | 28 Expect.equals(7, rnd.nextInt(i *= 2)); |
29 Expect.equals( 5, rnd.nextInt(i *= 2)); | 29 Expect.equals(5, rnd.nextInt(i *= 2)); |
30 Expect.equals( 29, rnd.nextInt(i *= 2)); | 30 Expect.equals(29, rnd.nextInt(i *= 2)); |
31 Expect.equals( 17, rnd.nextInt(i *= 2)); | 31 Expect.equals(17, rnd.nextInt(i *= 2)); |
32 Expect.equals( 104, rnd.nextInt(i *= 2)); | 32 Expect.equals(104, rnd.nextInt(i *= 2)); |
33 Expect.equals( 199, rnd.nextInt(i *= 2)); | 33 Expect.equals(199, rnd.nextInt(i *= 2)); |
34 Expect.equals( 408, rnd.nextInt(i *= 2)); | 34 Expect.equals(408, rnd.nextInt(i *= 2)); |
35 Expect.equals( 362, rnd.nextInt(i *= 2)); | 35 Expect.equals(362, rnd.nextInt(i *= 2)); |
36 Expect.equals( 995, rnd.nextInt(i *= 2)); | 36 Expect.equals(995, rnd.nextInt(i *= 2)); |
37 Expect.equals( 2561, rnd.nextInt(i *= 2)); | 37 Expect.equals(2561, rnd.nextInt(i *= 2)); |
38 Expect.equals( 2548, rnd.nextInt(i *= 2)); | 38 Expect.equals(2548, rnd.nextInt(i *= 2)); |
39 Expect.equals( 9553, rnd.nextInt(i *= 2)); | 39 Expect.equals(9553, rnd.nextInt(i *= 2)); |
40 Expect.equals( 2628, rnd.nextInt(i *= 2)); | 40 Expect.equals(2628, rnd.nextInt(i *= 2)); |
41 Expect.equals( 42376, rnd.nextInt(i *= 2)); | 41 Expect.equals(42376, rnd.nextInt(i *= 2)); |
42 Expect.equals( 101848, rnd.nextInt(i *= 2)); | 42 Expect.equals(101848, rnd.nextInt(i *= 2)); |
43 Expect.equals( 85153, rnd.nextInt(i *= 2)); | 43 Expect.equals(85153, rnd.nextInt(i *= 2)); |
44 Expect.equals( 495595, rnd.nextInt(i *= 2)); | 44 Expect.equals(495595, rnd.nextInt(i *= 2)); |
45 Expect.equals( 647122, rnd.nextInt(i *= 2)); | 45 Expect.equals(647122, rnd.nextInt(i *= 2)); |
46 Expect.equals( 793546, rnd.nextInt(i *= 2)); | 46 Expect.equals(793546, rnd.nextInt(i *= 2)); |
47 Expect.equals( 1073343, rnd.nextInt(i *= 2)); | 47 Expect.equals(1073343, rnd.nextInt(i *= 2)); |
48 Expect.equals( 4479969, rnd.nextInt(i *= 2)); | 48 Expect.equals(4479969, rnd.nextInt(i *= 2)); |
49 Expect.equals( 9680425, rnd.nextInt(i *= 2)); | 49 Expect.equals(9680425, rnd.nextInt(i *= 2)); |
50 Expect.equals( 28460171, rnd.nextInt(i *= 2)); | 50 Expect.equals(28460171, rnd.nextInt(i *= 2)); |
51 Expect.equals( 49481738, rnd.nextInt(i *= 2)); | 51 Expect.equals(49481738, rnd.nextInt(i *= 2)); |
52 Expect.equals( 9878974, rnd.nextInt(i *= 2)); | 52 Expect.equals(9878974, rnd.nextInt(i *= 2)); |
53 Expect.equals( 132552472, rnd.nextInt(i *= 2)); | 53 Expect.equals(132552472, rnd.nextInt(i *= 2)); |
54 Expect.equals( 210267283, rnd.nextInt(i *= 2)); | 54 Expect.equals(210267283, rnd.nextInt(i *= 2)); |
55 Expect.equals( 125422442, rnd.nextInt(i *= 2)); | 55 Expect.equals(125422442, rnd.nextInt(i *= 2)); |
56 Expect.equals( 226275094, rnd.nextInt(i *= 2)); | 56 Expect.equals(226275094, rnd.nextInt(i *= 2)); |
57 Expect.equals(1639629168, rnd.nextInt(i *= 2)); | 57 Expect.equals(1639629168, rnd.nextInt(i *= 2)); |
58 Expect.equals(0x100000000, i); | 58 Expect.equals(0x100000000, i); |
59 // If max is too large expect an ArgumentError. | 59 // If max is too large expect an ArgumentError. |
60 Expect.throws(() => rnd.nextInt(i + 1), (e) => e is ArgumentError); | 60 Expect.throws(() => rnd.nextInt(i + 1), (e) => e is ArgumentError); |
61 | 61 |
62 rnd = new Random(6790); | 62 rnd = new Random(6790); |
63 Expect.approxEquals(0.1202733131, rnd.nextDouble()); | 63 Expect.approxEquals(0.1202733131, rnd.nextDouble()); |
64 Expect.approxEquals(0.5554054805, rnd.nextDouble()); | 64 Expect.approxEquals(0.5554054805, rnd.nextDouble()); |
65 Expect.approxEquals(0.0385160727, rnd.nextDouble()); | 65 Expect.approxEquals(0.0385160727, rnd.nextDouble()); |
66 Expect.approxEquals(0.2836345217, rnd.nextDouble()); | 66 Expect.approxEquals(0.2836345217, rnd.nextDouble()); |
67 } | 67 } |
68 | 68 |
69 void checkSeed() { | 69 void checkSeed() { |
70 // Check that various seeds generate the expected first values. | 70 // Check that various seeds generate the expected first values. |
71 // 53 significant bits, so the number is representable in JS. | 71 // 53 significant bits, so the number is representable in JS. |
72 var rawSeed = 0x19a32c640e1d71; | 72 var rawSeed = 0x19a32c640e1d71; |
73 var expectations = [ | 73 var expectations = [ |
74 26007, 43006, 46458, 18610, 16413, 50455, 2164, 47399, 8859, 9732, | 74 26007, |
75 20367, 33935, 54549, 54913, 4819, 24198, 49353, 22277, 51852, 35959, | 75 43006, |
76 45347, 12100, 10136, 22372, 15293, 20066, 1351, 49030, 64845, 12793, | 76 46458, |
77 50916, 55784, 43170, 27653, 34696, 1492, 50255, 9597, 45929, 2874, | 77 18610, |
78 27629, 53084, 36064, 42140, 32016, 41751, 13967, 20516, 578, 16773, | 78 16413, |
79 53064, 14814, 22737, 48846, 45147, 10205, 56584, 63711, 44128, 21099, | 79 50455, |
80 47966, 35471, 39576, 1141, 45716, 54940, 57406, 15437, 31721, 35044, | 80 2164, |
81 28136, 39797, 50801, 22184, 58686 | 81 47399, |
| 82 8859, |
| 83 9732, |
| 84 20367, |
| 85 33935, |
| 86 54549, |
| 87 54913, |
| 88 4819, |
| 89 24198, |
| 90 49353, |
| 91 22277, |
| 92 51852, |
| 93 35959, |
| 94 45347, |
| 95 12100, |
| 96 10136, |
| 97 22372, |
| 98 15293, |
| 99 20066, |
| 100 1351, |
| 101 49030, |
| 102 64845, |
| 103 12793, |
| 104 50916, |
| 105 55784, |
| 106 43170, |
| 107 27653, |
| 108 34696, |
| 109 1492, |
| 110 50255, |
| 111 9597, |
| 112 45929, |
| 113 2874, |
| 114 27629, |
| 115 53084, |
| 116 36064, |
| 117 42140, |
| 118 32016, |
| 119 41751, |
| 120 13967, |
| 121 20516, |
| 122 578, |
| 123 16773, |
| 124 53064, |
| 125 14814, |
| 126 22737, |
| 127 48846, |
| 128 45147, |
| 129 10205, |
| 130 56584, |
| 131 63711, |
| 132 44128, |
| 133 21099, |
| 134 47966, |
| 135 35471, |
| 136 39576, |
| 137 1141, |
| 138 45716, |
| 139 54940, |
| 140 57406, |
| 141 15437, |
| 142 31721, |
| 143 35044, |
| 144 28136, |
| 145 39797, |
| 146 50801, |
| 147 22184, |
| 148 58686 |
82 ]; | 149 ]; |
83 var negative_seed_expectations = [ | 150 var negative_seed_expectations = [ |
84 12170, 42844, 39228, 64032, 29046, 57572, 8453, 52224, 27060, 28454, | 151 12170, |
85 20510, 28804, 59221, 53422, 11047, 50864, 33997, 19611, 1250, 65088, | 152 42844, |
86 19690, 11396, 20, 48867, 44862, 47129, 58724, 13325, 50005, 33320, | 153 39228, |
87 16523, 4740, 63721, 63272, 30545, 51403, 35845, 3943, 31850, 23148, | 154 64032, |
88 26307, 1724, 29281, 39988, 43653, 48012, 43810, 16755, 13105, 25325, | 155 29046, |
89 32648, 19958, 38838, 8322, 3421, 28624, 17269, 45385, 50680, 1696, | 156 57572, |
90 26088, 2787, 48566, 34357, 27731, 51764, 8455, 16498, 59721, 59568, | 157 8453, |
91 46333, 7935, 51459, 36766, 50711 | 158 52224, |
| 159 27060, |
| 160 28454, |
| 161 20510, |
| 162 28804, |
| 163 59221, |
| 164 53422, |
| 165 11047, |
| 166 50864, |
| 167 33997, |
| 168 19611, |
| 169 1250, |
| 170 65088, |
| 171 19690, |
| 172 11396, |
| 173 20, |
| 174 48867, |
| 175 44862, |
| 176 47129, |
| 177 58724, |
| 178 13325, |
| 179 50005, |
| 180 33320, |
| 181 16523, |
| 182 4740, |
| 183 63721, |
| 184 63272, |
| 185 30545, |
| 186 51403, |
| 187 35845, |
| 188 3943, |
| 189 31850, |
| 190 23148, |
| 191 26307, |
| 192 1724, |
| 193 29281, |
| 194 39988, |
| 195 43653, |
| 196 48012, |
| 197 43810, |
| 198 16755, |
| 199 13105, |
| 200 25325, |
| 201 32648, |
| 202 19958, |
| 203 38838, |
| 204 8322, |
| 205 3421, |
| 206 28624, |
| 207 17269, |
| 208 45385, |
| 209 50680, |
| 210 1696, |
| 211 26088, |
| 212 2787, |
| 213 48566, |
| 214 34357, |
| 215 27731, |
| 216 51764, |
| 217 8455, |
| 218 16498, |
| 219 59721, |
| 220 59568, |
| 221 46333, |
| 222 7935, |
| 223 51459, |
| 224 36766, |
| 225 50711 |
92 ]; | 226 ]; |
93 for (var i = 0, m = 1; i < 75; i++) { | 227 for (var i = 0, m = 1; i < 75; i++) { |
94 Expect.equals(expectations[i], new Random(rawSeed * m).nextInt(65536)); | 228 Expect.equals(expectations[i], new Random(rawSeed * m).nextInt(65536)); |
95 Expect.equals(negative_seed_expectations[i], | 229 Expect.equals( |
96 new Random(rawSeed * -m).nextInt(65536)); | 230 negative_seed_expectations[i], new Random(rawSeed * -m).nextInt(65536)); |
97 m *= 2; | 231 m *= 2; |
98 } | 232 } |
99 // And test zero seed too. | 233 // And test zero seed too. |
100 Expect.equals(21391, new Random(0).nextInt(65536)); | 234 Expect.equals(21391, new Random(0).nextInt(65536)); |
101 } | 235 } |
OLD | NEW |