Skip to main content

random_sample

Function random_sample 

Source
pub fn random_sample(low: i64, high: i64, length: usize) -> Result<Vec<i64>>
Expand description

Draws length distinct integers uniformly at random from the closed interval [low, high], returned in increasing order.

It uses Vitter’s sequential sampling algorithm (“Method D”), which runs in expected O(length) time and memory, regardless of the size of the interval: ideal to select a few edges out of a huge set of candidates. It draws from the thread’s default random number generator. An empty sample (length == 0) is always empty, also when low == high (igraph 1.0.0 and 1.0.1 return [low] there; the wrapper corrects this).

See also rng::shuffle for random permutations, and Graph::erdos_renyi_game_gnm, which picks its edges among all vertex pairs with the same method.

Reference: J. S. Vitter, An efficient algorithm for sequential random sampling, ACM Transactions on Mathematical Software 13(1):58–67, 1987.

Binds igraph_random_sample.

§Errors

ErrorKind::InvalidValue if low > high or length exceeds the size of the interval; ErrorKind::Overflow for a non-empty sample from an interval with more than i64::MAX elements.

§Examples

use igraph::{misc, prelude::*};
rng::seed(1)?;
let s = misc::random_sample(0, 1_000_000_000_000, 5)?;
assert_eq!(s.len(), 5);
assert!(s.windows(2).all(|w| w[0] < w[1]));