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 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 coin_test; | 8 library coin_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 seed = new Random().nextInt(1<<16); | 14 var seed = new Random().nextInt(1 << 16); |
15 print("coin_test seed: $seed"); | 15 print("coin_test seed: $seed"); |
16 var rnd = new Random(seed); | 16 var rnd = new Random(seed); |
17 var heads = 0; | 17 var heads = 0; |
18 var tails = 0; | 18 var tails = 0; |
19 for (var i = 0; i < 10000; i++) { | 19 for (var i = 0; i < 10000; i++) { |
20 if (rnd.nextBool()) { | 20 if (rnd.nextBool()) { |
21 heads++; | 21 heads++; |
22 } else { | 22 } else { |
23 tails++; | 23 tails++; |
24 } | 24 } |
25 } | 25 } |
26 print("Heads: $heads\n" | 26 print("Heads: $heads\n" |
27 "Tails: $tails\n" | 27 "Tails: $tails\n" |
28 "Ratio: ${heads/tails}\n"); | 28 "Ratio: ${heads/tails}\n"); |
29 Expect.approxEquals(1.0, heads/tails, 0.1); | 29 Expect.approxEquals(1.0, heads / tails, 0.1); |
30 | 30 |
31 heads = 0; | 31 heads = 0; |
32 tails = 0; | 32 tails = 0; |
33 for (var i = 0; i < 10000; i++) { | 33 for (var i = 0; i < 10000; i++) { |
34 rnd = new Random(i); | 34 rnd = new Random(i); |
35 if (rnd.nextBool()) { | 35 if (rnd.nextBool()) { |
36 heads++; | 36 heads++; |
37 } else { | 37 } else { |
38 tails++; | 38 tails++; |
39 } | 39 } |
40 } | 40 } |
41 print("Heads: $heads\n" | 41 print("Heads: $heads\n" |
42 "Tails: $tails\n" | 42 "Tails: $tails\n" |
43 "Ratio: ${heads/tails}\n"); | 43 "Ratio: ${heads/tails}\n"); |
44 Expect.approxEquals(1.0, heads/tails, 0.1); | 44 Expect.approxEquals(1.0, heads / tails, 0.1); |
45 | 45 |
46 // A sequence of newly allocated Random number generators should have fair | 46 // A sequence of newly allocated Random number generators should have fair |
47 // initial tosses. | 47 // initial tosses. |
48 heads = 0; | 48 heads = 0; |
49 tails = 0; | 49 tails = 0; |
50 for (var i = 0; i < 10000; i++) { | 50 for (var i = 0; i < 10000; i++) { |
51 rnd = new Random(); | 51 rnd = new Random(); |
52 if (rnd.nextBool()) { | 52 if (rnd.nextBool()) { |
53 heads++; | 53 heads++; |
54 } else { | 54 } else { |
55 tails++; | 55 tails++; |
56 } | 56 } |
57 } | 57 } |
58 print("Heads: $heads\n" | 58 print("Heads: $heads\n" |
59 "Tails: $tails\n" | 59 "Tails: $tails\n" |
60 "Ratio: ${heads/tails}\n"); | 60 "Ratio: ${heads/tails}\n"); |
61 Expect.approxEquals(1.0, heads/tails, 0.1); | 61 Expect.approxEquals(1.0, heads / tails, 0.1); |
62 } | 62 } |
OLD | NEW |