start rand utils suite
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
25c004f08c
commit
4f5c6de853
3 changed files with 28 additions and 12 deletions
24
src/core/utils/rand.rs
Normal file
24
src/core/utils/rand.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use std::{
|
||||
ops::Range,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
pub fn string(length: usize) -> String {
|
||||
thread_rng()
|
||||
.sample_iter(&rand::distributions::Alphanumeric)
|
||||
.take(length)
|
||||
.map(char::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn timepoint_secs(range: Range<u64>) -> SystemTime { SystemTime::now() + secs(range) }
|
||||
|
||||
#[must_use]
|
||||
pub fn secs(range: Range<u64>) -> Duration {
|
||||
let mut rng = thread_rng();
|
||||
Duration::from_secs(rng.gen_range(range))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue