Skip to main content

random_sample

Function random_sample 

Source
pub fn random_sample(
    n_samples: usize,
    sample_size: usize,
    replace: bool,
    random_seed: Option<u64>,
) -> Result<Vec<usize>>
Expand description

Performs random sampling with or without replacement

This function creates random samples from a dataset using either bootstrap sampling (with replacement) or standard random sampling (without replacement).

§Arguments

  • n_samples - Total number of samples in the dataset
  • sample_size - Number of samples to draw
  • replace - Whether to sample with replacement (bootstrap)
  • random_seed - Optional random seed for reproducible sampling

§Returns

A vector of indices representing the sampled data points

§Examples

use scirs2_datasets::utils::random_sample;

// Sample 5 indices from 10 total samples without replacement
let indices = random_sample(10, 5, false, Some(42)).expect("Operation failed");
assert_eq!(indices.len(), 5);
assert!(indices.iter().all(|&i| i < 10));

// Bootstrap sampling (with replacement)
let bootstrap_indices = random_sample(10, 15, true, Some(42)).expect("Operation failed");
assert_eq!(bootstrap_indices.len(), 15);