OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Test that rnd.nextInt with a seed generates the same sequence each time. | |
6 | |
7 // Library tag to allow Dartium to run the test. | |
8 library random_test; | |
9 | |
10 import "package:expect/expect.dart"; | |
11 import 'dart:math'; | |
12 | |
13 main() { | |
14 checkSequence(); | |
15 checkSeed(); | |
16 } | |
17 | |
18 void checkSequence() { | |
19 // Check the sequence of numbers generated by the random generator for a seed | |
20 // doesn't change unintendedly, and it agrees between implementations. | |
21 var rnd = new Random(20130307); | |
22 // Make sure we do not break the random number generation. | |
23 // If the random algorithm changes, make sure both the VM and dart2js | |
24 // generate the same new sequence. | |
25 var i = 1; | |
26 Expect.equals(0, rnd.nextInt(i *= 2)); | |
27 Expect.equals(3, rnd.nextInt(i *= 2)); | |
28 Expect.equals(7, rnd.nextInt(i *= 2)); | |
29 Expect.equals(5, rnd.nextInt(i *= 2)); | |
30 Expect.equals(29, rnd.nextInt(i *= 2)); | |
31 Expect.equals(17, rnd.nextInt(i *= 2)); | |
32 Expect.equals(104, rnd.nextInt(i *= 2)); | |
33 Expect.equals(199, rnd.nextInt(i *= 2)); | |
34 Expect.equals(408, rnd.nextInt(i *= 2)); | |
35 Expect.equals(362, rnd.nextInt(i *= 2)); | |
36 Expect.equals(995, rnd.nextInt(i *= 2)); | |
37 Expect.equals(2561, rnd.nextInt(i *= 2)); | |
38 Expect.equals(2548, rnd.nextInt(i *= 2)); | |
39 Expect.equals(9553, rnd.nextInt(i *= 2)); | |
40 Expect.equals(2628, rnd.nextInt(i *= 2)); | |
41 Expect.equals(42376, rnd.nextInt(i *= 2)); | |
42 Expect.equals(101848, rnd.nextInt(i *= 2)); | |
43 Expect.equals(85153, rnd.nextInt(i *= 2)); | |
44 Expect.equals(495595, rnd.nextInt(i *= 2)); | |
45 Expect.equals(647122, rnd.nextInt(i *= 2)); | |
46 Expect.equals(793546, rnd.nextInt(i *= 2)); | |
47 Expect.equals(1073343, rnd.nextInt(i *= 2)); | |
48 Expect.equals(4479969, rnd.nextInt(i *= 2)); | |
49 Expect.equals(9680425, rnd.nextInt(i *= 2)); | |
50 Expect.equals(28460171, rnd.nextInt(i *= 2)); | |
51 Expect.equals(49481738, rnd.nextInt(i *= 2)); | |
52 Expect.equals(9878974, rnd.nextInt(i *= 2)); | |
53 Expect.equals(132552472, rnd.nextInt(i *= 2)); | |
54 Expect.equals(210267283, rnd.nextInt(i *= 2)); | |
55 Expect.equals(125422442, rnd.nextInt(i *= 2)); | |
56 Expect.equals(226275094, rnd.nextInt(i *= 2)); | |
57 Expect.equals(1639629168, rnd.nextInt(i *= 2)); | |
58 Expect.equals(0x100000000, i); | |
59 // If max is too large expect an ArgumentError. | |
60 Expect.throws(() => rnd.nextInt(i + 1), (e) => e is ArgumentError); | |
61 | |
62 rnd = new Random(6790); | |
63 Expect.approxEquals(0.1202733131, rnd.nextDouble()); | |
64 Expect.approxEquals(0.5554054805, rnd.nextDouble()); | |
65 Expect.approxEquals(0.0385160727, rnd.nextDouble()); | |
66 Expect.approxEquals(0.2836345217, rnd.nextDouble()); | |
67 } | |
68 | |
69 void checkSeed() { | |
70 // Check that various seeds generate the expected first values. | |
71 // 53 significant bits, so the number is representable in JS. | |
72 var rawSeed = 0x19a32c640e1d71; | |
73 var expectations = [ | |
74 26007, | |
75 43006, | |
76 46458, | |
77 18610, | |
78 16413, | |
79 50455, | |
80 2164, | |
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 | |
149 ]; | |
150 var negative_seed_expectations = [ | |
151 12170, | |
152 42844, | |
153 39228, | |
154 64032, | |
155 29046, | |
156 57572, | |
157 8453, | |
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 | |
226 ]; | |
227 for (var i = 0, m = 1; i < 75; i++) { | |
228 Expect.equals(expectations[i], new Random(rawSeed * m).nextInt(65536)); | |
229 Expect.equals( | |
230 negative_seed_expectations[i], new Random(rawSeed * -m).nextInt(65536)); | |
231 m *= 2; | |
232 } | |
233 // And test zero seed too. | |
234 Expect.equals(21391, new Random(0).nextInt(65536)); | |
235 } | |
OLD | NEW |