Struct spinoso_random::Random[][src]

pub struct Random { /* fields omitted */ }

Random provides an interface to Ruby’s pseudo-random number generator, or PRNG.

The PRNG produces a deterministic sequence of bits which approximate true randomness. The sequence may be represented by integers, floats, or binary strings.

The generator may be initialized with either a system-generated or user-supplied seed value.

PRNGs are currently implemented as a modified Mersenne Twister with a period of 2**19937-1.

This RNG reproduces the same random bytes and floats as MRI. It may differ when returning elements confined to a distribution.

Examples

Create an RNG with a random seed:

let mut random = Random::new()?;
let next = random.next_int32();

Create a RNG with a fixed seed:

let seed = 5489_u32;
let mut random = Random::with_seed(seed);
let rand = random.next_int32();

let seed = [627457_u32, 697550, 16438, 41926];
let mut random = Random::with_array_seed(seed);
let rand = random.next_int32();

Implementations

impl Random[src]

pub fn new() -> Result<Self, InitializeError>[src]

Create a new Mersenne Twister random number generator with a randomly generated seed.

This method initializes the mersenne twister randome number generator with a seed derived from a cryptographically secure source of randomness.

Examples

let mut random = Random::new()?;
let next = random.next_int32();

Errors

If the randomness feature provided by the platform is not present or failed to completely generate a seed, an error is returned. This error should be raised as a Ruby RuntimeError.

#[must_use]pub fn with_seed(seed: u32) -> Self[src]

Create a new random number generator using the given seed.

Examples

let seed = 33;
let mut random = Random::with_seed(seed);
let rand = random.next_int32();

#[must_use]pub fn with_array_seed(seed: [u32; 4]) -> Self[src]

Create a new random number generator using the given seed.

Examples

let seed = [1_u32, 2, 3, 4];
let mut random = Random::with_array_seed(seed);
let rand = random.next_int32();

#[must_use]pub fn with_byte_array_seed(seed: [u8; 16]) -> Self[src]

Create a new random number generator using the given seed.

Examples

let seed = [1_u32, 2, 3, 4];
let mut random = Random::with_array_seed(seed);
let rand = random.next_int32();

#[must_use]pub fn next_int32(&mut self) -> u32[src]

Generate next u32 output.

Generates a random number on (0..=0xffffffff)-interval.

u32 is the native output of the generator. This function advances the RNG step counter by one.

Examples

let mut random = Random::new()?;
assert_ne!(random.next_int32(), random.next_int32());

#[must_use]pub fn next_real(&mut self) -> f64[src]

Generate next f64 output.

Generates a random number on [0,1) with 53-bit resolution.

Examples

let mut random = Random::new()?;
assert_ne!(random.next_real(), random.next_real());

pub fn fill_bytes(&mut self, dest: &mut [u8])[src]

Fill a buffer with bytes generated from the RNG.

This method generates random u32s (the native output unit of the RNG) until dest is filled.

This method may discard some output bits if dest.len() is not a multiple of 4.

Examples

let mut random = Random::new()?;
let mut buf = [0; 32];
random.fill_bytes(&mut buf);
assert_ne!([0; 32], buf);
let mut buf = [0; 31];
random.fill_bytes(&mut buf);
assert_ne!([0; 31], buf);

#[must_use]pub const fn seed(&self) -> [u32; 4][src]

Returns the seed value used to initialize the generator.

This may be used to initialize another generator with the same state at a later time, causing it to produce the same sequence of numbers.

Examples

let seed = [1_u32, 2, 3, 4];
let random = Random::with_array_seed(seed);
assert_eq!(random.seed(), seed);

Trait Implementations

impl Clone for Random[src]

impl Debug for Random[src]

impl Default for Random[src]

impl Eq for Random[src]

impl From<[u32; 4]> for Random[src]

impl From<[u8; 16]> for Random[src]

impl From<u32> for Random[src]

impl Hash for Random[src]

impl PartialEq<Random> for Random[src]

impl RngCore for Random[src]

#[must_use]fn next_u64(&mut self) -> u64[src]

Generate next u64 output.

This function is implemented by generating two u32s from the RNG and shifting + masking them into a u64 output.

Examples

let mut random = Random::with_seed(33);
assert_ne!(random.next_u64(), random.next_u64());

#[must_use]fn next_u32(&mut self) -> u32[src]

Generate next u32 output.

u32 is the native output of the generator. This function advances the RNG step counter by one.

Examples

let mut random = Random::with_seed(33);
assert_ne!(random.next_u32(), random.next_u32());

fn fill_bytes(&mut self, dest: &mut [u8])[src]

Fill a buffer with bytes generated from the RNG.

This method generates random u32s (the native output unit of the RNG) until dest is filled.

This method may discard some output bits if dest.len() is not a multiple of 4.

Examples

let mut random = Random::with_seed(33);
let mut buf = [0; 32];
random.fill_bytes(&mut buf);
assert_ne!([0; 32], buf);
let mut buf = [0; 31];
random.fill_bytes(&mut buf);
assert_ne!([0; 31], buf);

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>[src]

Fill a buffer with bytes generated from the RNG.

This method generates random u32s (the native output unit of the RNG) until dest is filled.

This method may discard some output bits if dest.len() is not a multiple of 4.

try_fill_bytes is implemented with fill_bytes and is infallible.

Examples

let mut random = Random::with_seed(33);
let mut buf = [0; 32];
random.try_fill_bytes(&mut buf)?;
assert_ne!([0; 32], buf);
let mut buf = [0; 31];
random.try_fill_bytes(&mut buf)?;
assert_ne!([0; 31], buf);

Errors

This method never returns an error. It is equivalent to calling the infallible fill_bytes method.

impl SeedableRng for Random[src]

type Seed = [u8; 16]

Seed type, which is restricted to types mutably-dereferencable as u8 arrays (we recommend [u8; N] for some N). Read more

fn from_seed(seed: Self::Seed) -> Self[src]

Reseed from four u32s.

Examples

// Default MT seed
let seed = 5489_u128.to_le_bytes();
let mut mt = Random::from_seed(seed);
assert_ne!(mt.next_u32(), mt.next_u32());

impl StructuralEq for Random[src]

impl StructuralPartialEq for Random[src]

Auto Trait Implementations

impl RefUnwindSafe for Random

impl Send for Random

impl Sync for Random

impl Unpin for Random

impl UnwindSafe for Random

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<R> Rng for R where
    R: RngCore + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 
[src]