fix: optimize random value generator

This commit is contained in:
Kilerd Chan 2018-11-22 10:39:11 +08:00
parent 39f715b90f
commit 4dcac21733

View File

@ -185,14 +185,15 @@ impl Token {
}
pub fn rand(lenth: i32) -> String {
use rand::Rng;
let token_string = TOKEN_SYMBOLS.to_string();
let len = TOKEN_SYMBOLS.len();
let mut ret = String::new();
let mut rng = rand::thread_rng();
for _ in 0..lenth {
let index = (rand::random::<i8>() as i8).abs() % 63;
ret.push(token_string.chars().nth(index as usize).unwrap());
let index: usize = rng.gen_range(0, len);
ret.push(token_string.chars().nth(index).unwrap());
}
ret
}
}