Code Explainers

Code explainers tagged #weighted-sampling

rust
use rand::Rng;
 
#[derive(Debug)]
pub struct WeightedChoice<T> {

Weighted random sampling in Rust

weighted-sampling generics binary-search
Intermediate 7 steps
ruby
class WeightedSampler
  def initialize(weights)
    @entries = weights.to_a
    @total = @entries.sum { |_, w| w }

Weighted random sampling with binary search

weighted-sampling binary-search cumulative-sum
Intermediate 7 steps