mirror of
https://github.com/GiriNeko/hbb_common.git
synced 2025-12-16 13:27:23 +00:00
time_based_rand
This commit is contained in:
parent
df95f44499
commit
44a7277827
1 changed files with 14 additions and 0 deletions
14
src/lib.rs
14
src/lib.rs
|
|
@ -447,6 +447,20 @@ pub fn version_check_request(typ: String) -> (VersionCheckRequest, String) {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn time_based_rand() -> u32 {
|
||||
let nanos = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
|
||||
let mut x = nanos as u64;
|
||||
x ^= x << 13;
|
||||
x ^= x >> 7;
|
||||
x ^= x << 17;
|
||||
|
||||
(x % 32768) as u32
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue