const N: usize = 10000;
fn main() {
let mut rand = urandom::new();
let mut buckets = [0u32; 8];
for _ in 0..N {
let float = rand.next_f32() - 1.0;
let bits = float.to_bits() & 0x7;
buckets[bits as usize] += 1;
}
print_buckets(&buckets, "naive implementation");
let mut buckets = [0u32; 8];
for _ in 0..N {
let float: f32 = rand.sample(&urandom::distr::Float01);
let bits = float.to_bits() & 0x7;
buckets[bits as usize] += 1;
}
print_buckets(&buckets, "Float01 distribution");
}
fn print_buckets(buckets: &[u32], name: &str) {
println!("\nThe low bits of {N} trials of the {name}:");
println!("```");
for (i, bucket) in buckets.iter().enumerate() {
println!("{i:>#05b}: {bucket}");
}
println!("```");
}