WeightedRandom.Backend.Linear (weighted_random v1.0.0-rc.1)

Copy Markdown View Source

This is a very naive approach. Quick and dirty to implement, but definitely not as fast as most other backends, especially at scale. One advantage it has is a very fast and simple preprocessing phase. So if you only need to run it once, this can actually beat the Walker Alias Method.

How it works

  1. Create a list in which every outcome is duplicated, a number of times equal to its weight.
  2. Higher weight outcomes appear more often in the list.
  3. During take, the list is passed into Enum.random/1