OLD | NEW |
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 // Test that the default PRNG does uniformly distribute values when not using | 5 // Test that the default PRNG does uniformly distribute values when not using |
6 // a power of 2. | 6 // a power of 2. |
7 | 7 |
8 // Library tag to allow Dartium to run the test. | 8 // Library tag to allow Dartium to run the test. |
9 library low_test; | 9 library low_test; |
10 | 10 |
11 import "package:expect/expect.dart"; | 11 import "package:expect/expect.dart"; |
12 import 'dart:math'; | 12 import 'dart:math'; |
13 | 13 |
14 void main() { | 14 void main() { |
15 var n = (2 * (1<<32)) ~/ 3; | 15 var n = (2 * (1 << 32)) ~/ 3; |
16 var n2 = n ~/ 2; | 16 var n2 = n ~/ 2; |
17 | 17 |
18 var iterations = 200000; | 18 var iterations = 200000; |
19 | 19 |
20 var seed = new Random().nextInt(1<<16); | 20 var seed = new Random().nextInt(1 << 16); |
21 print("low_test seed: $seed"); | 21 print("low_test seed: $seed"); |
22 var prng = new Random(seed); | 22 var prng = new Random(seed); |
23 | 23 |
24 var low = 0; | 24 var low = 0; |
25 for (var i = 0; i < iterations; i++) { | 25 for (var i = 0; i < iterations; i++) { |
26 if (prng.nextInt(n) < n2) { | 26 if (prng.nextInt(n) < n2) { |
27 low++; | 27 low++; |
28 } | 28 } |
29 } | 29 } |
30 | 30 |
31 var diff = (low - (iterations ~/ 2)).abs(); | 31 var diff = (low - (iterations ~/ 2)).abs(); |
32 print("$low, $diff"); | 32 print("$low, $diff"); |
33 Expect.isTrue(diff < (iterations ~/ 20)); | 33 Expect.isTrue(diff < (iterations ~/ 20)); |
34 } | 34 } |
OLD | NEW |