Skip to main content

sample_uniform_batch

Function sample_uniform_batch 

Source
pub fn sample_uniform_batch<F>(
    n: usize,
    low: F,
    high: F,
    seed: Option<u64>,
) -> StatsResult<Array1<F>>
Expand description

Generate n independent samples from U(low, high) using SIMD rescaling.

Each sample is drawn from the standard uniform U(0, 1) and then linearly rescaled:

x = low + u · (high − low)

The rescaling is performed as a SIMD FMA pass over the full batch, giving vectorised throughput equal to the underlying RNG bottleneck.

§Arguments

  • n — Number of samples.
  • low — Lower bound of the interval (inclusive).
  • high — Upper bound of the interval (exclusive).
  • seed — Optional RNG seed.

§Errors

Returns StatsError::InvalidArgument when n == 0 or low >= high.

§Examples

use scirs2_stats::sample_uniform_batch;

let samples = sample_uniform_batch::<f64>(500, 2.0, 5.0, Some(7))
    .expect("sampling failed");
assert_eq!(samples.len(), 500);
assert!(samples.iter().all(|&x| x >= 2.0 && x < 5.0));