https://github.com/thara/elixir_alias_method
Implementation of Walker's Alias method in Elixir
https://github.com/thara/elixir_alias_method
algorithm alias-method elixir walker-alias
Last synced: 2 days ago
JSON representation
Implementation of Walker's Alias method in Elixir
- Host: GitHub
- URL: https://github.com/thara/elixir_alias_method
- Owner: thara
- License: mit
- Created: 2021-10-10T16:27:37.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-10-11T15:36:51.000Z (over 4 years ago)
- Last Synced: 2025-11-21T17:10:44.867Z (3 months ago)
- Topics: algorithm, alias-method, elixir, walker-alias
- Language: Elixir
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AliasMethod
[](https://hex.pm/packages/alias_method) [](https://github.com/thara/elixir_alias_method/actions/workflows/elixir.yml)
Implementation of Walker's Alias method by Elixir.
The algorithm is principally useful when you need to random sampling with replacement by O(1).
## Installation
```elixir
def deps do
[
{:alias_method, "~> 0.1.0"}
]
end
```
## Example
```elixir
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
```
## License
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details
## Author
*Tomochika Hara* - [thara](https://github.com/thara)