Struct randomize::PCG32K

source ·
pub struct PCG32K<const K: usize> {
    pub state: u64,
    pub ext: [u32; K],
}
Expand description

A Permuted Congruential Generator with 32-bit output, extended to K dimensions.

This is like the PCG32, but replaces the single inc field with an extension array of K elements. At each step of the generator a different element of the array is XOR-ed with the output, and every time the generator passes 0 the array is advanced. Given enough time, the entire extension array will increment through all values (as if it were a 32*K-bit number). This gives a radically larger generator period than the basic PCG32, and guarantees that any sequence of K outputs in a row will appear at least once within the generator’s full output stream. Of course it’s highly unlikely that your program will ever advance the generator through all possible states, but just the possibility that a given output sequence will definitely eventually occur can be comfort enough.

For best results, K should be a power of 2. The type works with other K values, but when K is a power of 2 then selecting an element from the extension array is significantly faster (a bit mask instead of an integer division).

  • Period: 2**(64+32*k)

Fields§

§state: u64

The generator’s state.

This changes with each step of the generator. It’s the generator’s “position” within the output stream.

§ext: [u32; K]

The generator’s extension array. The generator’s base output is XOR’d with random elements of this array to determine the final output at each step.

Implementations§

source§

impl<const K: usize> PCG32K<K>

source

pub const fn new(state: u64, ext: [u32; K]) -> Self

Creates a new generator by directly using the value given.

source

pub fn from_getrandom() -> Result<Self, Error>

Available on crate feature getrandom only.

Create a new generator seeded with data from getrandom.

Failure
  • If the getrandom call fails the error bubbles up.
source

pub fn scramble_ext_array(&mut self) -> Result<(), Error>

Available on crate feature getrandom only.

Runs getrandom on the extension array.

This will completely scramble the generator’s position within the output sequence.

Failure
  • If the getrandom call fails the error bubbles up.
source

pub fn next_u32(&mut self) -> u32

Generate the next u32 in the sequence.

Trait Implementations§

source§

impl<const K: usize> Clone for PCG32K<K>

source§

fn clone(&self) -> PCG32K<K>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<const K: usize> Debug for PCG32K<K>

source§

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

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

impl<const K: usize> Gen32 for PCG32K<K>

source§

fn next_u32(&mut self) -> u32

Makes the generator create the next output. Read more
source§

fn next_i32(&mut self) -> i32

Gives a uniformly distributed value.
source§

fn next_bool(&mut self) -> bool

Gives a uniformly distributed value.
source§

fn next_f32_unit(&mut self) -> f32

Gives a value in the range 0.0 ..= 1.0
source§

fn d4(&mut self) -> i32

Gives a value in the range 1 ..= 4
source§

fn d6(&mut self) -> i32

Gives a value in the range 1 ..= 6
source§

fn d8(&mut self) -> i32

Gives a value in the range 1 ..= 8
source§

fn d10(&mut self) -> i32

Gives a value in the range 1 ..= 10
source§

fn d12(&mut self) -> i32

Gives a value in the range 1 ..= 12
source§

fn d20(&mut self) -> i32

Gives a value in the range 1 ..= 20

Auto Trait Implementations§

§

impl<const K: usize> RefUnwindSafe for PCG32K<K>

§

impl<const K: usize> Send for PCG32K<K>

§

impl<const K: usize> Sync for PCG32K<K>

§

impl<const K: usize> Unpin for PCG32K<K>

§

impl<const K: usize> UnwindSafe for PCG32K<K>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.