| 1 | import rand |
| 2 | |
| 3 | struct Histo { |
| 4 | mut: |
| 5 | lo f64 |
| 6 | ct int |
| 7 | } |
| 8 | |
| 9 | // The sample size to be used; keep cpu time less than 5 seconds |
| 10 | const count = 9100100 |
| 11 | // Two sets of seeds |
| 12 | const seeds = [[u32(2742798260), 2159764996], [u32(2135051596), 958016781]] |
| 13 | |
| 14 | fn test_f32() { |
| 15 | mut histo := []Histo{} |
| 16 | mut candidate := f32(0.0) |
| 17 | |
| 18 | histo << Histo{1.0e-9, 0} |
| 19 | histo << Histo{1.0e-6, 0} |
| 20 | histo << Histo{1.0e-4, 0} |
| 21 | histo << Histo{1.0e-2, 0} |
| 22 | histo << Histo{1.0e00, 0} |
| 23 | |
| 24 | for seed in seeds { |
| 25 | rand.seed(seed) |
| 26 | for _ in 0 .. count { |
| 27 | candidate = rand.f32() |
| 28 | for mut p in histo { |
| 29 | if candidate < p.lo { |
| 30 | p.ct += 1 |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | println(' f32 ') |
| 36 | println(histo) |
| 37 | assert histo[0].ct == 4 |
| 38 | assert histo[1].ct == 21 |
| 39 | assert histo[2].ct == 1821 |
| 40 | assert histo[3].ct == 182583 |
| 41 | assert histo[4].ct == 18200200 |
| 42 | for mut p in histo { |
| 43 | p.ct = 0 |
| 44 | } |
| 45 | for seed in seeds { |
| 46 | rand.seed(seed) |
| 47 | for _ in 0 .. count { |
| 48 | candidate = rand.f32cp() |
| 49 | for mut p in histo { |
| 50 | if candidate < p.lo { |
| 51 | p.ct += 1 |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | println(' f32cp') |
| 57 | println(histo) |
| 58 | assert histo[0].ct == 0 |
| 59 | assert histo[1].ct == 22 |
| 60 | assert histo[2].ct == 1829 |
| 61 | assert histo[3].ct == 142203 |
| 62 | assert histo[4].ct == 18200200 |
| 63 | } |
| 64 | |
| 65 | fn test_f64() { |
| 66 | mut histo := []Histo{} |
| 67 | mut candidate := f64(0.0) |
| 68 | |
| 69 | histo << Histo{1.0e-9, 0} |
| 70 | histo << Histo{1.0e-6, 0} |
| 71 | histo << Histo{1.0e-4, 0} |
| 72 | histo << Histo{1.0e-2, 0} |
| 73 | histo << Histo{1.0e00, 0} |
| 74 | |
| 75 | for seed in seeds { |
| 76 | rand.seed(seed) |
| 77 | for _ in 0 .. count { |
| 78 | candidate = rand.f64() |
| 79 | for mut p in histo { |
| 80 | if candidate < p.lo { |
| 81 | p.ct += 1 |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | println(' f64 ') |
| 87 | println(histo) |
| 88 | assert histo[0].ct == 0 |
| 89 | assert histo[1].ct == 25 |
| 90 | assert histo[2].ct == 1763 |
| 91 | assert histo[3].ct == 182552 |
| 92 | assert histo[4].ct == 18200200 |
| 93 | for mut p in histo { |
| 94 | p.ct = 0 |
| 95 | } |
| 96 | for seed in seeds { |
| 97 | rand.seed(seed) |
| 98 | for _ in 0 .. count { |
| 99 | candidate = rand.f64cp() |
| 100 | for mut p in histo { |
| 101 | if candidate < p.lo { |
| 102 | p.ct += 1 |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | println(' f64cp') |
| 108 | println(histo) |
| 109 | assert histo[0].ct == 0 |
| 110 | assert histo[1].ct == 22 |
| 111 | assert histo[2].ct == 1787 |
| 112 | assert histo[3].ct == 182160 |
| 113 | assert histo[4].ct == 18200200 |
| 114 | } |
| 115 | |