1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-12 17:22:43 +00:00
doukutsu-rs/src/rng.rs

24 lines
544 B
Rust
Raw Normal View History

2020-08-21 05:27:26 +00:00
use std::cell::Cell;
pub struct RNG(Cell<i32>);
2020-08-20 18:31:47 +00:00
impl RNG {
2020-08-21 05:27:26 +00:00
pub fn new(seed: i32) -> Self {
Self(Cell::new(seed))
2020-08-20 18:31:47 +00:00
}
2020-08-21 05:27:26 +00:00
pub fn next(&self) -> i32 {
2020-08-20 18:31:47 +00:00
// MSVC LCG values
2020-08-21 05:27:26 +00:00
self.0.replace(self.0.get().wrapping_mul(214013).wrapping_add(2531011));
self.0.get()
}
pub fn next_u32(&self) -> u32 {
self.next() as u32
2020-08-20 18:31:47 +00:00
}
2020-08-21 05:27:26 +00:00
pub fn range(&self, range: std::ops::Range<i32>) -> i32 {
range.start.wrapping_add(self.next() % (range.end.wrapping_sub(range.start).wrapping_add(1)))
2020-08-20 18:31:47 +00:00
}
}