Skip to main content

Rng

Struct Rng 

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

The xoshiro256** generator (256 bits of state).

Implementations§

Source§

impl Rng

Source

pub fn new(seed: u64) -> Rng

A generator seeded from one u64 through SplitMix64.

Source

pub fn state(&self) -> [u64; 4]

The 256 bits of the state, to save a generator and resume it with Rng::from_state (the pickling of the Python generator).

Source

pub fn from_state(s: [u64; 4]) -> Result<Rng, Error>

The generator of a state saved by Rng::state: it continues the draws of the saved generator.

§Errors

crate::Error::Config for the all-zero state, a fixed point of xoshiro256** that no seed gives.

Source

pub fn next_u64(&mut self) -> u64

Next 64 random bits.

Source

pub fn uniform(&mut self) -> f64

Uniform on [0, 1) with 53 random bits.

Source

pub fn below(&mut self, n: usize) -> usize

Uniform integer in 0..n (n ≥ 1).

§Panics

In debug builds, for n = 0 (an empty range has no element: release builds return 0 rather than check it in the inner loops).

Source

pub fn phase(&mut self) -> f64

A phase uniform on $[-\pi, \pi)$: $2\pi U - \pi$, $U$ of Rng::uniform (one draw; the initial phases of the fading generators).

§Examples
use comnum_core::rng::Rng;
let mut r = Rng::new(1);
assert!((0..1000).map(|_| r.phase()).all(|p| (-std::f64::consts::PI..std::f64::consts::PI).contains(&p)));
Source

pub fn bit(&mut self) -> u8

One fair random bit.

Source

pub fn gaussian(&mut self) -> f64

Standard Gaussian N(0, 1).

Source

pub fn complex_gaussian(&mut self) -> Complex<f64>

Circular complex Gaussian with E|z|² = 1 (each component of variance ½).

Source

pub fn gamma(&mut self, shape: f64) -> f64

A gamma variable of shape shape and unit scale (mean shape), by the method of Marsaglia & Tsang (2000): for a shape below 1, the draw of shape shape + 1 times $U^{1/\text{shape}}$. A non-positive or non-finite shape returns NaN.

§Examples
use comnum_core::rng::Rng;
let mut r = Rng::new(1);
let n = 100_000;
let mean = (0..n).map(|_| r.gamma(2.5)).sum::<f64>() / n as f64;
assert!((mean - 2.5).abs() < 0.03);
Source

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

A uniform random permutation of v in place (Fisher–Yates, Durstenfeld’s version): for $i = n - 1, \dots, 1$, swap $v_{i}$ and $v_{j}$ with $j$ uniform in $\{0, \dots, i\}$ (Rng::below), $n - 1$ draws in all.

§Examples
use comnum_core::rng::Rng;
let mut v: Vec<usize> = (0..10).collect();
Rng::new(3).shuffle(&mut v);
let mut sorted = v.clone();
sorted.sort_unstable();
assert_eq!(sorted, (0..10).collect::<Vec<_>>());
Source

pub fn complex_noise(&mut self, n: usize, noise_var: f64) -> Vec<Complex<f64>>

n circular complex Gaussian samples of variance noise_var (E|z|² = σ²).

Trait Implementations§

Source§

impl Clone for Rng

Source§

fn clone(&self) -> Rng

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 Rng

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Rng

§

impl Freeze for Rng

§

impl RefUnwindSafe for Rng

§

impl Send for Rng

§

impl Sync for Rng

§

impl Unpin for Rng

§

impl UnsafeUnpin for Rng

§

impl UnwindSafe for Rng

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.