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>
pub fn from_state(s: [u64; 4]) -> Result<Rng>
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) -> C64
pub fn complex_gaussian(&mut self) -> C64
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<_>>());