| 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 a coin toss with Random.nextBool() is fair. | 5 // Test that a coin toss with Random.nextBool() is fair. |
| 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 var rnd = new Random(20130307); | 14 var rnd = new Random(20130307); |
| 15 // Make sure we do not break the random number generation. | 15 // Make sure we do not break the random number generation. |
| 16 var i = 0; | 16 var i = 0; |
| 17 Expect.equals( 0, rnd.nextInt(1 << ++i)); | 17 Expect.equals( 1, rnd.nextInt(1 << ++i)); |
| 18 Expect.equals( 2, rnd.nextInt(1 << ++i)); | 18 Expect.equals( 1, rnd.nextInt(1 << ++i)); |
| 19 Expect.equals( 7, rnd.nextInt(1 << ++i)); | 19 Expect.equals( 7, rnd.nextInt(1 << ++i)); |
| 20 Expect.equals( 8, rnd.nextInt(1 << ++i)); | 20 Expect.equals( 6, rnd.nextInt(1 << ++i)); |
| 21 Expect.equals( 1, rnd.nextInt(1 << ++i)); | 21 Expect.equals( 6, rnd.nextInt(1 << ++i)); |
| 22 Expect.equals( 61, rnd.nextInt(1 << ++i)); | 22 Expect.equals( 59, rnd.nextInt(1 << ++i)); |
| 23 Expect.equals( 31, rnd.nextInt(1 << ++i)); | 23 Expect.equals( 11, rnd.nextInt(1 << ++i)); |
| 24 Expect.equals( 230, rnd.nextInt(1 << ++i)); | 24 Expect.equals( 212, rnd.nextInt(1 << ++i)); |
| 25 Expect.equals( 390, rnd.nextInt(1 << ++i)); | 25 Expect.equals( 17, rnd.nextInt(1 << ++i)); |
| 26 Expect.equals( 443, rnd.nextInt(1 << ++i)); | 26 Expect.equals( 507, rnd.nextInt(1 << ++i)); |
| 27 Expect.equals( 1931, rnd.nextInt(1 << ++i)); | 27 Expect.equals( 1060, rnd.nextInt(1 << ++i)); |
| 28 Expect.equals( 3028, rnd.nextInt(1 << ++i)); | 28 Expect.equals( 891, rnd.nextInt(1 << ++i)); |
| 29 Expect.equals( 5649, rnd.nextInt(1 << ++i)); | 29 Expect.equals( 1534, rnd.nextInt(1 << ++i)); |
| 30 Expect.equals( 4603, rnd.nextInt(1 << ++i)); | 30 Expect.equals( 8404, rnd.nextInt(1 << ++i)); |
| 31 Expect.equals( 27684, rnd.nextInt(1 << ++i)); | 31 Expect.equals( 13839, rnd.nextInt(1 << ++i)); |
| 32 Expect.equals( 54139, rnd.nextInt(1 << ++i)); | 32 Expect.equals( 23298, rnd.nextInt(1 << ++i)); |
| 33 Expect.equals( 83454, rnd.nextInt(1 << ++i)); | 33 Expect.equals( 53622, rnd.nextInt(1 << ++i)); |
| 34 Expect.equals( 106708, rnd.nextInt(1 << ++i)); | 34 Expect.equals( 205997, rnd.nextInt(1 << ++i)); |
| 35 Expect.equals( 112143, rnd.nextInt(1 << ++i)); | 35 Expect.equals( 393823, rnd.nextInt(1 << ++i)); |
| 36 Expect.equals( 875266, rnd.nextInt(1 << ++i)); | 36 Expect.equals( 514614, rnd.nextInt(1 << ++i)); |
| 37 Expect.equals( 971126, rnd.nextInt(1 << ++i)); | 37 Expect.equals( 233715, rnd.nextInt(1 << ++i)); |
| 38 Expect.equals( 1254573, rnd.nextInt(1 << ++i)); | 38 Expect.equals( 895357, rnd.nextInt(1 << ++i)); |
| 39 Expect.equals( 4063839, rnd.nextInt(1 << ++i)); | 39 Expect.equals( 4726185, rnd.nextInt(1 << ++i)); |
| 40 Expect.equals( 7854646, rnd.nextInt(1 << ++i)); | 40 Expect.equals( 7976427, rnd.nextInt(1 << ++i)); |
| 41 Expect.equals( 29593843, rnd.nextInt(1 << ++i)); | |
| 42 Expect.equals( 17672573, rnd.nextInt(1 << ++i)); | |
| 43 Expect.equals( 80223657, rnd.nextInt(1 << ++i)); | |
| 44 Expect.equals( 142194155, rnd.nextInt(1 << ++i)); | |
| 45 Expect.equals( 31792146, rnd.nextInt(1 << ++i)); | 41 Expect.equals( 31792146, rnd.nextInt(1 << ++i)); |
| 46 Expect.equals(1042196170, rnd.nextInt(1 << ++i)); | 42 Expect.equals( 35563210, rnd.nextInt(1 << ++i)); |
| 47 Expect.equals(1589656273, rnd.nextInt(1 << ++i)); | 43 Expect.equals( 113261265, rnd.nextInt(1 << ++i)); |
| 48 Expect.equals(1547294578, rnd.nextInt(1 << ++i)); | 44 Expect.equals( 205117298, rnd.nextInt(1 << ++i)); |
| 45 Expect.equals( 447729735, rnd.nextInt(1 << ++i)); |
| 46 Expect.equals(1072507596, rnd.nextInt(1 << ++i)); |
| 47 Expect.equals(2134030067, rnd.nextInt(1 << ++i)); |
| 48 Expect.equals(721180690, rnd.nextInt(1 << ++i)); |
| 49 Expect.equals(32, i); | 49 Expect.equals(32, i); |
| 50 // If max is too large expect an ArgumentError. | 50 // If max is too large expect an ArgumentError. |
| 51 Expect.throws(() => rnd.nextInt((1 << i)+1), (e) => e is ArgumentError); | 51 Expect.throws(() => rnd.nextInt((1 << i)+1), (e) => e is ArgumentError); |
| 52 } | 52 } |
| OLD | NEW |