Skip to content

Commit c549fe2

Browse files
thejpsterjonathanpallant
authored andcommitted
Rand example also prints min/max
1 parent e01ca02 commit c549fe2

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

examples/rand.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
//! Show the output of the random number generator
2+
3+
use core::ffi::c_int;
4+
15
fn main() {
26
let mut distribution = std::collections::BTreeMap::new();
7+
let mut min = c_int::MAX;
8+
let mut max = c_int::MIN;
39

410
let mut seedp = 1;
5-
for _ in 0..4 {
6-
println!(".");
11+
for i in 0..40 {
12+
println!("{}/40", i);
713
for _ in 0..1_000_000 {
814
let random = unsafe { tinyrlibc::rand_r(&mut seedp) };
15+
if random > max {
16+
max = random;
17+
}
18+
if random < min {
19+
min = random;
20+
}
921
for place in 0..core::ffi::c_int::BITS {
1022
let random_bit = (random & (1 << place)) != 0;
1123
if random_bit {
@@ -18,7 +30,9 @@ fn main() {
1830
}
1931
}
2032

33+
println!("Min value: {min:#10x}");
34+
println!("Max value: {max:#10x}");
2135
for (k, v) in distribution.iter() {
22-
println!("{:02} => {}", k, v);
36+
println!("Bit {:02} was set {} times", k, v);
2337
}
2438
}

0 commit comments

Comments
 (0)