ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - JohnJocoo/weighted_random: Elixir weighted random pick library optimised for quick take_one and take_n operations
Elixir weighted random pick library optimised for quick take_one and take_n operations - JohnJocoo/weighted_random
Visit Site

GitHub - JohnJocoo/weighted_random: Elixir weighted random pick library optimised for quick take_one and take_n operations

GitHub - JohnJocoo/weighted_random: Elixir weighted random pick library optimised for quick take_one and take_n operations

WeightedRandom

Elixir weighted random pick library optimised for quick take_one and take_n operations.

take_one\1 and take_n\2 behaviours are compatible with Enum.random\1 and Enum.take_random\2 and can almost be drop-in replacements, except that WeightedRandom accepts elements in format {element :: any(), weight :: number()}.

Installation

The package can be installed by adding better_weighted_random to your list of dependencies in mix.exs:

def deps do
  [
    {:better_weighted_random, "~> 0.1.0"}
  ]
end

Usage

WeightedRandom.take_one\1 returns one random element from input, with probability based on weights.

iex> :rand.seed(:exsss, {100, 101, 102})
iex> WeightedRandom.take_one([{:'1', 0.5}, {:'2', 1.0}, {:'3', 2.0}])
:"3"

WeightedRandom.take_n\1 returns n random elements from input, with probability based on weights. Similar to Enum.take_random\2 element can not be picked twice.

iex> :rand.seed(:exsss, {100, 101, 102})
iex> WeightedRandom.take_n([{:'1', 0.5}, {:'2', 1.0}, {:'3', 2.0}], 2)
[:"3", :"1"]

Probability and weight

Probability of picking an element directly depends on its weight. Probability = element_weight / sum(all_weights)

iex> :rand.seed(:exsss, {100, 101, 102})
iex> Enum.map(1..1000, fn _ -> WeightedRandom.take_one([{:'1', 1.0}, {:'2', 2.0}, {:'3', 1.0}]) end) |> Enum.frequencies()
%{
  "1": 231, 
  "2": 522, 
  "3": 247
} 

Searcher and quick lookups

You can create searcher with WeightedRandom.create_searcher\1 for optimised lookups with take_one\1 and take_n\2.

iex> searcher = WeightedRandom.create_searcher([{:'1', 1.0}, {:'2', 2.0}, {:'3', 1.0}])

iex> :rand.seed(:exsss, {100, 101, 102})
iex> WeightedRandom.take_one(searcher)
:"3"

iex> rand.seed(:exsss, {100, 101, 102})
iex> WeightedRandom.take_n(searcher, 2)
[:"3", :"1"]

Using searcher is quicker if you do repetative calls.

elements = Enum.map(1..10000, fn i -> {:"el#{i}", :rand.uniform()} end)
searcher = WeightedRandom.create_searcher(elements)

WeightedRandom.take_one/1

Name                             ips        average  deviation         median         99th %
take_one with searcher      787.69 K     0.00127 ms  ±2423.14%     0.00109 ms     0.00180 ms
take_one with list           0.137 K        7.29 ms    ±17.57%        7.03 ms       11.42 ms

Comparison:
take_one with searcher      787.69 K
take_one with list           0.137 K - 5740.48x slower +7.29 ms

WeightedRandom.take_n/2 take 100 elements

Name                               ips        average  deviation         median         99th %
take_n 100 with searcher        7.26 K       0.138 ms    ±30.77%       0.124 ms        0.32 ms
take_n 100 with list           0.106 K        9.43 ms    ±12.97%        8.96 ms       12.29 ms

Comparison:
take_n 100 with searcher        7.26 K
take_n 100 with list           0.106 K - 68.46x slower +9.30 ms

Docs

Docs are availiable at https://hexdocs.pm/better_weighted_random

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory