Skip to main content

Prng

Struct Prng 

Source
pub struct Prng { /* private fields */ }
Expand description

A small, deterministic pseudo-random generator that lives inside game state.

The whole reproducible position is a single u64 (Prng::position), so the generator is Copy, serializes with the state it lives in (O(1) snapshot and resume), and a Reversible game’s undo record can restore its position exactly. Everything is integer-only with a fixed algorithm, so a seed reproduces the same stream on every platform. Do not rely on the concrete algorithm (currently PCG XSH-RR 64/32, single stream): it is an implementation detail of this newtype and may change.

This is not cryptographically secure and must not be used for security.

Implementations§

Source§

impl Prng

Source

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

Creates a generator seeded from seed.

Equal seeds produce identical streams; different seeds are extremely likely to diverge immediately.

Source

pub const fn position(&self) -> u64

Returns the generator’s current position in its stream.

Capture this before a move and pass it to Prng::set_position to rewind, which is how a make/unmake (Reversible) undo record restores the random stream.

Source

pub const fn set_position(&mut self, position: u64)

Restores a position previously read from Prng::position.

Source

pub const fn next_u32(&mut self) -> u32

Returns the next 32-bit value and advances the stream by one step.

Source

pub const fn next_u64(&mut self) -> u64

Returns the next 64-bit value (two steps of the stream).

Source

pub const fn below(&mut self, bound: u64) -> u64

Returns a uniformly distributed value in 0..bound.

Uses rejection sampling to avoid modulo bias, so a single call consumes a variable number of steps. That is why a pre-move Prng::position must be snapshotted rather than reconstructed by counting draws.

§Panics

Panics if bound is zero.

Source

pub const fn range(&mut self, low: u64, high: u64) -> u64

Returns a uniformly distributed value in low..high.

§Panics

Panics if low >= high.

Source

pub fn choose<'a, T>(&mut self, items: &'a [T]) -> Option<&'a T>

Returns a reference to a uniformly chosen element, or None if empty.

Source

pub const fn shuffle<T>(&mut self, items: &mut [T])

Shuffles items in place with an unbiased Fisher-Yates shuffle.

Trait Implementations§

Source§

impl Clone for Prng

Source§

fn clone(&self) -> Prng

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Prng

Source§

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

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

impl<'de> Deserialize<'de> for Prng

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Hash for Prng

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Prng

Source§

fn eq(&self, other: &Prng) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Prng

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Prng

Source§

impl Eq for Prng

Source§

impl StructuralPartialEq for Prng

Auto Trait Implementations§

§

impl Freeze for Prng

§

impl RefUnwindSafe for Prng

§

impl Send for Prng

§

impl Sync for Prng

§

impl Unpin for Prng

§

impl UnsafeUnpin for Prng

§

impl UnwindSafe for Prng

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,