View Source WeightedRandom (weighted_random v0.4.2)

Summary

Functions

Returns a random value based on the weights given.

Functions

Returns a random value based on the weights given.

By default this operates on the index, not the value.

Examples

iex> :rand.seed(:exsss, {108, 101, 102})
iex> li = 1..10
iex> weights = [ %{target: 7, weight: 100} ]
iex>
iex> # By default this uses the index 7, not the *value* 7.
iex> WeightedRandom.rand(li, weights)
8
iex> # But we can use the value by passing the option index: false
iex> WeightedRandom.rand(li, weights, index: false)
7
iex> li = [:a, :b, :c, :d, :e, :f, :g, :h, :j, :k, :l]
iex> WeightedRandom.rand(li, weights)
:h
iex> weights = [ %{target: :d, weight: 100} ]
iex> WeightedRandom.rand(li, weights, index: false)
:d