Skip to main content

Module rng

Module rng 

Source
Expand description

Random number generation (igraph_random.h).

Every randomized igraph function (the random graph generators of games, randomized community detection, layouts with a random start, Vector::shuffle, …) draws from the calling thread’s default random number generator. Each thread gets its own (a PCG32 generator, igraph’s default algorithm, seeded randomly the first time the thread calls into igraph), so threads never share random state, and seed makes the results of a thread reproducible, whatever the other threads do.

The generators are igraph’s own, so a seed gives the very numbers that igraph’s C test suite pins (tests/unit/igraph_rng_get_integer.out):

use igraph::prelude::*;

rng::seed(42).unwrap();
let a: Vec<i64> = (0..10).map(|_| rng::integer(10, 100)).collect();
assert_eq!(a, vec![22, 59, 86, 99, 99, 77, 93, 12, 20, 62]);
rng::seed(42).unwrap();
let b: Vec<i64> = (0..10).map(|_| rng::integer(10, 100)).collect();
assert_eq!(a, b);

// Random graphs are reproducible too.
rng::seed(7).unwrap();
let g = Graph::erdos_renyi_game_gnp(30, 0.2, false, EdgeTypeSw::Simple, false).unwrap();
rng::seed(7).unwrap();
let h = Graph::erdos_renyi_game_gnp(30, 0.2, false, EdgeTypeSw::Simple, false).unwrap();
assert_eq!(g, h);

Independent generators of a chosen RngType are created as owned Rng values, and can be installed as the default for the duration of a closure with Rng::scoped.

C function (igraph_random.h)Default generatorOwned Rng
igraph_rng_init / _destroy(per thread, automatic)Rng::new / Drop
igraph_rng_seedseedRng::set_seed
igraph_rng_name, _bits, _maxname, bits, maxRng::name, Rng::bits, Rng::max
(the is_seeded field)Rng::is_seeded
igraph_rng_get_integerintegerRng::get_integer
igraph_rng_get_unif(01)uniform, uniform01Rng::get_unif, Rng::get_unif01
igraph_rng_get_boolbooleanRng::get_bool
igraph_rng_get_normalnormalRng::get_normal
igraph_rng_get_geom, _binom, _exp, _gamma, _poisgeometric, binomial, exponential, gamma, poissonRng::get_geom, Rng::get_binom, Rng::get_exp, Rng::get_gamma, Rng::get_pois
igraph_rng_default, igraph_rng_set_defaultRng::scoped
(Fisher-Yates in Rust)shuffle

See also games for random graph models, and misc::sample_sphere_surface, misc::sample_sphere_volume and misc::sample_dirichlet (also available as methods of an owned Rng) for random points and misc::random_sample for sampling integers without replacement.

Enums§

RngType
The random number generator algorithms shipped with igraph.

Functions§

binomial
A binomially distributed random number: the number of successes in n independent trials of success probability p, i.e. k with probability C(n, k) p^k (1 - p)^(n - k) (igraph_rng_get_binom).
bits
Number of random bits produced at once by the default generator (igraph_rng_bits).
boolean
A random boolean, true with probability 1/2 (igraph_rng_get_bool).
exponential
An exponentially distributed random number with rate rate (mean 1 / rate); NaN for a non-positive rate, 0 for an infinite one (igraph_rng_get_exp).
gamma
A gamma distributed random number with density proportional to x^(shape - 1) exp(-x / scale) (mean shape * scale) (igraph_rng_get_gamma).
geometric
A geometrically distributed random number (failures before the first success) with success probability p in (0, 1]; NaN for an invalid p (igraph_rng_get_geom).
integer
A uniform random integer in the closed interval [min, max] (igraph_rng_get_integer).
max
The largest integer the default generator can produce natively (igraph_rng_max).
name
The name of the algorithm of the thread’s default generator (igraph_rng_name), e.g. "PCG64".
normal
A normally distributed random real with mean mean and standard deviation sd, i.e. with density proportional to exp(-(x - mean)² / (2 sd²)) (igraph_rng_get_normal).
poisson
A Poisson distributed random number with mean mean; NaN if mean is negative or NaN (igraph_rng_get_pois).
seed
Seeds the default random number generator of the calling thread (igraph_rng_seed).
shuffle
Shuffles a slice in place (Fisher-Yates) with the default generator, so that the outcome is reproducible with seed; the slice counterpart of Vector::shuffle (igraph_vector_shuffle), which runs the same algorithm: for the same seed both produce the same permutation.
uniform
A uniform random real in [min, max) (min itself if min == max) (igraph_rng_get_unif).
uniform01
A uniform random real in the half-open interval [0, 1) (igraph_rng_get_unif01).

Type Aliases§

Rng
An owned random number generator (igraph_rng_t).