Skip to main content

random_sample

Function random_sample 

Source
pub fn random_sample(
    l: i64,
    h: i64,
    length: usize,
    seed: u64,
) -> IgraphResult<Vec<i64>>
Expand description

Generate an increasing random sequence of integers from [l, h].

Returns a sorted Vec<i64> of length distinct integers sampled uniformly without replacement from the inclusive interval [l, h]. The algorithm is Vitter’s Algorithm D (1987), which runs in expected O(length) time regardless of the interval size.

§Arguments

  • l — lower bound of the sampling interval (inclusive).
  • h — upper bound of the sampling interval (inclusive).
  • length — number of integers to sample.
  • seed — seed for the internal PRNG (deterministic for a given seed).

§Errors

Returns InvalidArgument if:

  • l > h (empty interval).
  • length exceeds the number of integers in [l, h].

§Examples

use rust_igraph::random_sample;

// Sample 5 integers from [1, 100]
let sample = random_sample(1, 100, 5, 42).unwrap();
assert_eq!(sample.len(), 5);
// Result is sorted ascending
for w in sample.windows(2) {
    assert!(w[0] < w[1]);
}
// All values in range
for &v in &sample {
    assert!(v >= 1 && v <= 100);
}