pub struct Rng { /* private fields */ }Expand description
The xoshiro256** generator (256 bits of state).
Implementations§
Source§impl Rng
impl Rng
Sourcepub fn state(&self) -> [u64; 4]
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).
Sourcepub fn from_state(s: [u64; 4]) -> Result<Rng, Error>
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.
Sourcepub fn below(&mut self, n: usize) -> usize
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).
Sourcepub fn phase(&mut self) -> f64
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)));Sourcepub fn complex_gaussian(&mut self) -> Complex<f64>
pub fn complex_gaussian(&mut self) -> Complex<f64>
Circular complex Gaussian with E|z|² = 1 (each component of variance ½).
Sourcepub fn gamma(&mut self, shape: f64) -> f64
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);Sourcepub fn shuffle<T>(&mut self, v: &mut [T])
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<_>>());Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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