use rand::rng;
use rand::Rng;
use std::collections::HashSet;
fn find_second_max(total: usize, rndmax: u32) -> Option<u32> {
let mut rng_instance = rng();
let mut numbers = Vec::with_capacity(total);
for _ in 0..total {
let n = rng_instance.random_range(1..=rndmax);
println!("{}", n);
numbers.push(n);
}
let mut unique_numbers: Vec<u32> = HashSet::<u32>::from_iter(numbers).into_iter().collect();
unique_numbers.sort_unstable_by(|a, b| b.cmp(a));
unique_numbers.get(1).copied()
}
fn main() {
let second_max = find_second_max(10, 100);
match second_max {
Some(value) => println!("The second biggest number is: {}", value),
None => println!("Not enough unique numbers to determine a second maximum."),
}
}
/*
run:
29
52
37
1
38
84
64
35
99
55
The second biggest number is: 84
*/