https://github.com/ikeikeikeike/wakaway
There're §Walker's Alias Method§ and §Weighted Choice§ that providing weighted random choice algorism in two ways.
https://github.com/ikeikeikeike/wakaway
Last synced: 3 months ago
JSON representation
There're §Walker's Alias Method§ and §Weighted Choice§ that providing weighted random choice algorism in two ways.
- Host: GitHub
- URL: https://github.com/ikeikeikeike/wakaway
- Owner: ikeikeikeike
- License: mit
- Created: 2016-07-19T09:34:33.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-07-21T02:35:31.000Z (almost 9 years ago)
- Last Synced: 2025-02-20T02:45:30.728Z (4 months ago)
- Language: Elixir
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Wakaway
[](http://travis-ci.org/ikeikeikeike/wakaway)
[](https://hex.pm/packages/wakaway)
[](https://hex.pm/packages/wakaway)
[](http://inch-ci.org/github/ikeikeikeike/wakaway)
[](https://github.com/ikeikeikeike/wakaway/blob/master/LICENSE)There're `Walker's Alias Method` and `Weighted Choice` that providing weighted random choice algorism in two ways.
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed as:
1. Add `wakaway` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:wakaway, "~> 0.5.0"}]
end
```## Usage
#### WalkersAliasMethod
```elixir
alias Wakaway.WalkersAliasMethodresource =
WalkersAliasMethod.resource(
["k1", "k2", "k3", "k50", "k100", "k200"],
[1, 2, 3, 50, 100, 200]
)# resource =
# [s100: 100, s200: 200, s50: 50, s3: 3, s2: 2, s1: 1,]
# |> WalkersAliasMethod.resource# resource =
# %{100 => 100, 200 => 200, 50 => 50, 3 => 3, 2 => 2, 1 => 1}
# |> WalkersAliasMethod.resourceresult =
Enum.map(0..50, fn _ ->
WalkersAliasMethod.choice(resource)
end)IO.inspect result
# ["k50", "k200", "k200", "k200", "k200", "k50", "k200", "k200", "k100", "k200", "k200", "k100", "k200", "k100", "k200", "k200",
# "k200", "k200", "k200", "k200", "k200", "k100", "k200", "k200", "k200", "k50", "k200", "k200", "k200", "k50", "k200", "k200",
# "k200", "k200", "k100", "k100", "k200", "k100", "k200", "k100", "k200", "k100", "k100", "k50", "k200", "k50", "k200", "k200",
# "k200", ...]
```#### WeightedChoice
```elixir
alias Wakaway.WeightedChoiceresource =
WeightedChoice.resource(
["k1", "k2", "k3", "k50", "k100", "k200"],
[1, 2, 3, 50, 100, 200]
)# resource =
# [s100: 100, s200: 200, s50: 50, s3: 3, s2: 2, s1: 1,]
# |> WeightedChoice.resource# resource =
# %{100 => 100, 200 => 200, 50 => 50, 3 => 3, 2 => 2, 1 => 1}
# |> WeightedChoice.resourceresult =
Enum.map(0..50, fn _ ->
WeightedChoice.choice(resource)
end)IO.inspect result
# ["k50", "k200", "k200", "k200", "k200", "k50", "k200", "k200", "k100", "k200", "k200", "k100", "k200", "k100", "k200", "k200",
# "k200", "k200", "k200", "k200", "k200", "k100", "k200", "k200", "k200", "k50", "k200", "k200", "k200", "k50", "k200", "k200",
# "k200", "k200", "k100", "k100", "k200", "k100", "k200", "k100", "k200", "k100", "k100", "k50", "k200", "k50", "k200", "k200",
# "k200", ...]
```#### Simple ways
###### NOTE: This examples are slower than above way when they are called by users a lot of times.
```elixir
iex(1)> item = %{"k100" => 100, "k200" => 200, "k50" => 50, "k3" => 3, "k2" => 2, "1k" => 1}
%{"k100" => 100, "k200" => 200, "k50" => 50, "k3" => 3, "k2" => 2, "1k" => 1}
iex(2)> Wakaway.walker_choice(item)
"k200"
iex(3)> Wakaway.weighted_choice(item)
"k100"
```[Example more](https://github.com/ikeikeikeike/wakaway/blob/master/test/wakaway_test.exs)