pub struct igraph_rng_t { /* private fields */ }Implementations§
Source§impl igraph_rng_t
impl igraph_rng_t
Sourcepub fn new(kind: RngType, seed: u64) -> Result<Self>
pub fn new(kind: RngType, seed: u64) -> Result<Self>
Creates a new generator of the given type, seeded with seed
(igraph_rng_init and
igraph_rng_seed).
Two generators of the same type and seed produce the same numbers, on any thread:
use igraph::prelude::*;
// Pinned by igraph's own tests (`rng_init_destroy_max_bits_name_set_default.out`).
let mut mt = Rng::new(RngType::Mt19937, 42).unwrap();
let draws: Vec<i64> = (0..5).map(|_| mt.get_integer(0, 100)).collect();
assert_eq!(draws, vec![37, 80, 96, 18, 73]);§Errors
ErrorKind::OutOfMemory if
the state cannot be allocated.
Sourcepub fn set_seed(&mut self, seed: u64) -> Result<()>
pub fn set_seed(&mut self, seed: u64) -> Result<()>
Re-seeds this generator
(igraph_rng_seed): the numbers it produces
afterwards depend only on its type and on seed.
Sourcepub fn name(&self) -> String
pub fn name(&self) -> String
The name of the algorithm, e.g. "MT19937", "LIBC", "PCG32" or
"PCG64" (igraph_rng_name).
Sourcepub fn get_integer(&mut self, min: i64, max: i64) -> i64
pub fn get_integer(&mut self, min: i64, max: i64) -> i64
A uniform random integer in the closed interval [min, max]
(igraph_rng_get_integer).
§Panics
If min > max.
Sourcepub fn get_unif(&mut self, min: f64, max: f64) -> f64
pub fn get_unif(&mut self, min: f64, max: f64) -> f64
A uniform random real in [min, max)
(igraph_rng_get_unif).
§Panics
If a bound is not finite, max - min overflows, or min > max.
Sourcepub fn get_unif01(&mut self) -> f64
pub fn get_unif01(&mut self) -> f64
A uniform random real in [0, 1)
(igraph_rng_get_unif01).
Sourcepub fn get_normal(&mut self, mean: f64, sd: f64) -> f64
pub fn get_normal(&mut self, mean: f64, sd: f64) -> f64
A normally distributed random real with mean mean and standard
deviation sd
(igraph_rng_get_normal).
Sourcepub fn get_bool(&mut self) -> bool
pub fn get_bool(&mut self) -> bool
A random boolean (igraph_rng_get_bool).
Sourcepub fn get_geom(&mut self, p: f64) -> f64
pub fn get_geom(&mut self, p: f64) -> f64
A geometric random number: the number of failures before the first
success, with success probability p
(igraph_rng_get_geom).
Sourcepub fn get_binom(&mut self, n: i64, p: f64) -> f64
pub fn get_binom(&mut self, n: i64, p: f64) -> f64
A binomial random number with n trials of probability p
(igraph_rng_get_binom).
Sourcepub fn get_exp(&mut self, rate: f64) -> f64
pub fn get_exp(&mut self, rate: f64) -> f64
An exponential random number with the given rate
(igraph_rng_get_exp).
Sourcepub fn get_gamma(&mut self, shape: f64, scale: f64) -> f64
pub fn get_gamma(&mut self, shape: f64, scale: f64) -> f64
A gamma random number with the given shape and scale
(igraph_rng_get_gamma).
Sourcepub fn get_pois(&mut self, mean: f64) -> f64
pub fn get_pois(&mut self, mean: f64) -> f64
A Poisson random number with the given mean
(igraph_rng_get_pois).
Sourcepub fn bits(&self) -> u32
pub fn bits(&self) -> u32
Number of random bits produced at once by this generator
(igraph_rng_bits).
Sourcepub fn max(&self) -> u64
pub fn max(&self) -> u64
The largest integer this generator produces natively
(igraph_rng_max).
Sourcepub fn is_seeded(&self) -> bool
pub fn is_seeded(&self) -> bool
Whether the generator has been seeded (always true for generators
created with Rng::new): the is_seeded flag that
igraph_rng_seed sets, and that igraph_setup checks before seeding
the default generator.
Sourcepub fn scoped<R>(&mut self, f: impl FnOnce() -> R) -> R
pub fn scoped<R>(&mut self, f: impl FnOnce() -> R) -> R
Runs f with this generator installed as the thread’s default one
(igraph_rng_set_default), restoring the
previous default afterwards, even if f panics.
Every randomized igraph function called by f on this thread (and
the free functions of this module) then draws from self; other
threads are not affected.
use igraph::prelude::*;
let mut rng = Rng::new(RngType::Mt19937, 7).unwrap();
let x = rng.scoped(|| rng::integer(0, 1_000_000));
let mut again = Rng::new(RngType::Mt19937, 7).unwrap();
assert_eq!(again.scoped(|| rng::integer(0, 1_000_000)), x);Source§impl igraph_rng_t
impl igraph_rng_t
Sourcepub fn sample_sphere_surface(
&mut self,
dim: usize,
n: usize,
radius: f64,
positive: bool,
) -> Result<Vec<Vec<f64>>>
pub fn sample_sphere_surface( &mut self, dim: usize, n: usize, radius: f64, positive: bool, ) -> Result<Vec<Vec<f64>>>
Like sample_sphere_surface,
drawing from this generator (igraph_rng_sample_sphere_surface).
use igraph::prelude::*;
let mut a = Rng::new(RngType::Pcg64, 5)?;
let mut b = Rng::new(RngType::Pcg64, 5)?;
assert_eq!(a.sample_sphere_surface(4, 3, 1.0, false)?, b.sample_sphere_surface(4, 3, 1.0, false)?);Sourcepub fn sample_sphere_volume(
&mut self,
dim: usize,
n: usize,
radius: f64,
positive: bool,
) -> Result<Vec<Vec<f64>>>
pub fn sample_sphere_volume( &mut self, dim: usize, n: usize, radius: f64, positive: bool, ) -> Result<Vec<Vec<f64>>>
Like sample_sphere_volume,
drawing from this generator (igraph_rng_sample_sphere_volume).
Sourcepub fn sample_dirichlet(
&mut self,
n: usize,
alpha: &[f64],
) -> Result<Vec<Vec<f64>>>
pub fn sample_dirichlet( &mut self, n: usize, alpha: &[f64], ) -> Result<Vec<Vec<f64>>>
Like sample_dirichlet, drawing from
this generator (igraph_rng_sample_dirichlet).