use crate::{WallSwitchError, WallSwitchResult};
use std::hash::{BuildHasher, Hasher, RandomState};
pub trait RandomExt {
fn shuffle(&mut self);
}
impl<T> RandomExt for [T] {
fn shuffle(&mut self) {
let n: usize = self.len();
if n < 2 {
return;
}
for i in 0..(n - 1) {
let j = (rand() as usize) % (n - i) + i;
self.swap(i, j);
}
}
}
pub fn rand() -> u64 {
RandomState::new().build_hasher().finish()
}
pub fn get_random_integer(min: u64, max: u64) -> u64 {
if min >= max {
return min;
}
min + rand() % (max - min + 1)
}
pub fn get_random_integer_safe(min: u64, max: u64) -> WallSwitchResult<u64> {
if min > max {
Err(WallSwitchError::MinMax { min, max })
} else {
Ok(min + rand() % (max - min + 1))
}
}