pub struct WeightedSampler { /* private fields */ }Expand description
Pre-computed weighted sampler for O(log n) repeated sampling.
Builds a cumulative distribution table from weights, then uses binary search for each sample.
§Algorithm
CDF-based weighted sampling with binary search.
§Complexity
- Construction: O(n)
- Sampling: O(log n)
§Examples
use u_numflow::random::{create_rng, WeightedSampler};
let weights = vec![1.0, 2.0, 3.0, 4.0];
let sampler = WeightedSampler::new(&weights).unwrap();
let mut rng = create_rng(42);
let idx = sampler.sample(&mut rng);
assert!(idx < 4);Implementations§
Source§impl WeightedSampler
impl WeightedSampler
Auto Trait Implementations§
impl Freeze for WeightedSampler
impl RefUnwindSafe for WeightedSampler
impl Send for WeightedSampler
impl Sync for WeightedSampler
impl Unpin for WeightedSampler
impl UnsafeUnpin for WeightedSampler
impl UnwindSafe for WeightedSampler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more