AliasMethod (Alias Method v0.1.0)

Implementation of Walker's Alias method by Elixir. The algorithm is principally useful when you need to random sampling with replacement by O(1).

## Examples

  iex> weights = [1, 1, 8]
  iex> table = AliasMethod.generate_table(weights)
  iex> table
  %AliasMethod.Table{alias_table: %{0 => 2, 1 => 2, 2 => 0}, length: 3, probability_table: %{0 => 0.3, 1 => 0.3, 2 => 1.0}}
  iex> n = AliasMethod.choice(table)
  iex> 0 <= n && n <= table.length
  true

Link to this section Summary

Functions

Choice a index from passed Table at random.

Generate Table by weights.

Link to this section Functions

Specs

choice(AliasMethod.Table.t()) :: integer()

Choice a index from passed Table at random.

Link to this function

generate_table(weights)

Specs

generate_table([float()]) :: AliasMethod.Table.t()

Generate Table by weights.