use super::primes::PRIMES;
pub trait RandomSource {
fn next_bits(&mut self, bits: u32) -> i32;
fn next_double(&mut self) -> f64 {
let hi = i64::from(self.next_bits(26));
let lo = i64::from(self.next_bits(27));
#[allow(clippy::cast_precision_loss, reason = "exact: the value has 53 bits")]
let n = ((hi << 27) + lo) as f64;
n * DOUBLE_UNIT
}
fn next_int_bounded(&mut self, bound: i32) -> Option<i32> {
if bound <= 0 {
return None;
}
let mut r = self.next_bits(31);
let m = bound - 1;
if bound & m == 0 {
#[allow(clippy::cast_possible_truncation, reason = "result < bound")]
return Some(((i64::from(bound) * i64::from(r)) >> 31) as i32);
}
let mut u = r;
loop {
r = u % bound;
if u.wrapping_sub(r).wrapping_add(m) < 0 {
u = self.next_bits(31);
} else {
return Some(r);
}
}
}
fn next_long(&mut self) -> i64 {
let hi = i64::from(self.next_bits(32));
let lo = i64::from(self.next_bits(32));
(hi << 32).wrapping_add(lo)
}
fn next_prime(&mut self) -> i32 {
let len = PRIMES.len();
let mut index = len;
while index == len {
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss,
reason = "0 <= nextDouble * len < len"
)]
{
index = (self.next_double() * len as f64).floor() as usize;
}
}
PRIMES[index]
}
fn next_index(&mut self, size: u64) -> u64 {
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss,
reason = "0 <= nextDouble * size < size; sizes past 2^53 lose precision \
deterministically (dive mode only: TLC sizes are ints)"
)]
let i = (self.next_double() * size as f64).floor() as u64;
i
}
}
const DOUBLE_UNIT: f64 = f64::EPSILON / 2.0;
impl<F: FnMut(u32) -> i32 + ?Sized> RandomSource for F {
fn next_bits(&mut self, bits: u32) -> i32 {
self(bits)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct JavaRandom {
state: u64,
}
const MULTIPLIER: u64 = 0x0005_DEEC_E66D;
const ADDEND: u64 = 0xB;
const MASK: u64 = (1 << 48) - 1;
impl JavaRandom {
#[must_use]
pub const fn new(seed: i64) -> Self {
Self {
state: Self::scramble(seed),
}
}
pub const fn set_seed(&mut self, seed: i64) {
self.state = Self::scramble(seed);
}
#[allow(
clippy::cast_sign_loss,
reason = "two's complement bits, as Java's long"
)]
const fn scramble(seed: i64) -> u64 {
(seed as u64 ^ MULTIPLIER) & MASK
}
pub fn set_seed_aril(&mut self, seed: i64, aril: u64) {
self.set_seed(seed);
for _ in 0..aril {
let _ = self.next_double();
}
}
}
impl RandomSource for JavaRandom {
fn next_bits(&mut self, bits: u32) -> i32 {
debug_assert!((1..=32).contains(&bits));
self.state = self.state.wrapping_mul(MULTIPLIER).wrapping_add(ADDEND) & MASK;
#[allow(clippy::cast_possible_truncation, reason = "Java's (int) cast")]
let v = (self.state >> (48 - bits)) as u32;
v.cast_signed()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn matches_java_for_seed_7() {
let mut r = JavaRandom::new(7);
let got: Vec<u64> = (0..10).map(|_| r.next_index(1_000_000)).collect();
assert_eq!(
got,
[
730_699, 749_169, 348_309, 897_277, 708_177, 351_914, 120_736, 849_913, 83_219,
928_748
]
);
}
#[test]
fn bad_bound_draws_nothing() {
let mut r = JavaRandom::new(1);
let before = r;
assert_eq!(r.next_int_bounded(0), None);
assert_eq!(r.next_int_bounded(-5), None);
assert_eq!(r, before);
}
}