Skip to main content
← PSoXide SDK guides and examples · SDK f2fec1c301f1 · All Cargo features enabled; check feature gates before use.

LcgRng

Struct LcgRng 

Source
pub struct LcgRng(/* private fields */);
Expand description

32-bit integer LCG. Step once per next() / signed() call.

Implementations§

Source§

impl LcgRng

Source

pub const fn new(seed: u32) -> Self

Build with an explicit seed. Same seed → same sequence.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub const fn state(self) -> u32

Current internal state – useful if a caller wants to save / restore the RNG across reset boundaries.

Trait Implementations§

Source§

impl Clone for LcgRng

Source§

fn clone(&self) -> LcgRng

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LcgRng

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for LcgRng

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 T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.