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