-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexercise_5_1_3.rs
38 lines (31 loc) · 967 Bytes
/
exercise_5_1_3.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
pub fn random<F: FnMut() -> bool>(mut biased_random: F) -> bool {
loop {
let x = biased_random();
let y = biased_random();
if x != y {
return x;
}
}
}
#[cfg(test)]
mod tests {
use rand::distributions::{Bernoulli, Distribution};
#[test]
fn test_random() {
let test_count = 100_000;
let mut rng = rand::thread_rng();
for p_times_10 in 3..=7 {
let distribution = Bernoulli::new(f64::from(p_times_10) / 10.0).unwrap();
let mut biased_random = || distribution.sample(&mut rng);
let mut diff: i32 = 0;
for _ in 0..test_count {
if super::random(&mut biased_random) {
diff += 1;
} else {
diff -= 1;
}
}
assert!((f64::from(diff.abs()) / f64::from(test_count)) < 0.02); // Does not guarantee to success.
}
}
}