use rand::{rng, Rng};
pub fn uuid() -> String {
uuid::Uuid::new_v4().to_string()
}
pub fn verification_code(len: usize) -> String {
let text = "0123456789";
let mut code = "".to_string();
for _ in 0..len {
let index = random_number(0, text.len() - 1);
let t = &text[index..=index];
code = format!("{}{}", code, t);
}
code.to_string()
}
pub fn random_password(len: usize) -> String {
let text = "abcdefghijklmnopqrstuvwxyz0123456789";
let mut code = "".to_string();
for _ in 0..len {
let index = random_number(0, text.len() - 1);
let t = &text[index..=index];
code = format!("{}{}", code, t);
}
code.to_string()
}
pub fn random_number(begin: usize, finish: usize) -> usize {
let mut rng = rng();
rng.random_range(begin..=finish) as usize
}
pub fn random_number_f64(begin: f64, finish: f64) -> f64 {
let mut rng = rng();
rng.random_range(begin..=finish) as f64
}
#[cfg(test)]
mod tests {
use crate::random::{random_number, random_number_f64, random_password, uuid, verification_code};
#[test]
fn random() {
let res = uuid();
println!("uuid: {}", res);
let res = verification_code(4);
println!("verification_code: {}", res);
let res = random_password(4);
println!("verification_code: {}", res);
let res = random_number(1,50);
println!("verification_code: {}", res);
let res = random_number_f64(0.1,10.9);
println!("verification_code: {}", res);
}
}