pub struct LcgRng(/* private fields */);Expand description
32-bit integer LCG. Step once per next() / signed() call.
Implementations§
Source§impl LcgRng
impl LcgRng
Sourcepub fn next(&mut self) -> u32
pub fn next(&mut self) -> u32
One LCG step. Multiplier + increment are glibc’s constants.
Returns the fresh internal state.
§Take the high bits, not the low ones
The low bits of any power-of-two LCG are very weak, and bit 0 of this
one is not random at all. The multiplier and the increment are both
odd, so x' = x*m + c gives x'0 = x0 ^ 1: bit 0 strictly alternates,
period two. Bit k has period at most 2^(k+1).
So next() & 1 ping-pongs, next() & 127 cycles inside 128 draws, and
next() % 25 leans on the same weak bits. Use LcgRng::next_mixed
for anything that masks or takes a remainder, or shift the high half
down yourself as LcgRng::signed does.
Sourcepub fn next_mixed(&mut self) -> u32
pub fn next_mixed(&mut self) -> u32
One step with the strong high half folded down over the weak low half, for callers that mask or take a remainder.
VoXide worked this out and carried it as a private wrapper: its callers
lean on & 1, % 4 and % 120, and the raw low bits cycle with tiny
periods. It belongs on the generator rather than in one game.
Sourcepub fn below(&mut self, max: u32) -> u32
pub fn below(&mut self, max: u32) -> u32
Uniform-ish value in [0, max), or 0 when max is 0.
Sourced from LcgRng::next_mixed, so it is safe against the low-bit
weakness a bare next() % max walks into.
Sourcepub fn signed(&mut self, range: i16) -> i16
pub fn signed(&mut self, range: i16) -> i16
Signed integer in [-range, +range], symmetric about zero, sourced from
five bits of the LCG: thirty-two evenly spaced values from -range to
+range, rounded toward zero. Any range is fine, negative included
(the result is then mirrored); the arithmetic is 32-bit, so it does not
overflow for the whole i16 range.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for LcgRng
impl RefUnwindSafe for LcgRng
impl Send for LcgRng
impl Sync for LcgRng
impl Unpin for LcgRng
impl UnsafeUnpin for LcgRng
impl UnwindSafe for LcgRng
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)