pub fn prs_with_rng<F, R: Rng + Clone>(
    f: F,
    n: usize,
    (a, b): (f64, f64),
    m: usize,
    eps: f64,
    rng: &mut R,
) -> anyhow::Result<Vec<f64>>
where
    F: Fn(f64) -> f64 + Copy,
{
    let mut result = vec![0f64; n];

    let w = WeightedUniform::from_max_pool_1d(f, (a, b), m, eps)?;

    let mut initial_x = w.sample_with_rng(rng, n);
    let mut left_num = n;

    while left_num > 0 {
        for &x in initial_x.iter() {
            let weight = w.weight_at(x);
            if weight <= 0f64 {
                continue;
            } else {
                let y = rng.random_range(0f64..=weight);

                if y <= f(x) {
                    result[n - left_num] = x;
                    left_num -= 1;
                    if left_num == 0 {
                        return Ok(result);
                    }
                }
            }
        }
        initial_x = w.sample_with_rng(rng, left_num);
    }
    panic!("Error: failed to generate {} samples", n);
}
Piecewise Rejection Sampling with specific Rng

# Arguments
* `f` - Function to sample (unnormalized function is allowed)
* `n` - Number of samples
* `(a, b)` - Range of sampling
* `m` - Number of pieces
* `eps` - Epsilon for max pooling
* `rng` - Random number generator

# Examples
```
use peroxide::fuga::*;

fn main() -> Result<(), Box<dyn Error>> {
let mut rng = smallrng_from_seed(42);
let f = |x: f64| {
if (0f64..=2f64).contains(&x) {
-(x - 1f64).powi(2) + 1f64
} else {
0f64
}
};

let samples = prs_with_rng(f, 1000, (-1f64, 3f64), 200, 1e-4, &mut rng)?;
assert!((samples.mean() - 1f64).abs() < 1e-1);

Ok(())
}
src/statistics/rand.rs