WeightedRandom (better_weighted_random v0.1.0)

Module to take random elements from enumerable based on their weights.

Summary

Functions

Turn enumerable of {element, weight} to structure optimised for search (take_* operations).

Take N elements from enumerable of {element, weight} or from structure returned by create_searcher/1.

Take one element from enumerable of {element, weight} or from structure returned by create_searcher/1.

Types

@type element() :: any()
Link to this type

enumerable(_type)

@type enumerable(_type) :: Enumerable.t()
@type searcher() :: %WeightedRandom{
  number_elements: term(),
  search_table: term(),
  total_weight: term()
}
@type weight() :: number()

Functions

Link to this function

create_searcher(enumerable)

@spec create_searcher(enumerable({element(), weight()})) :: searcher()

Turn enumerable of {element, weight} to structure optimised for search (take_* operations).

Link to this function

take_n(enumerable_or_searcher, number)

@spec take_n(enumerable({element(), weight()}) | searcher(), integer()) :: [element()]

Take N elements from enumerable of {element, weight} or from structure returned by create_searcher/1.

Link to this function

take_one(enumerable_or_searcher)

@spec take_one(enumerable({element(), weight()}) | searcher()) :: element() | nil

Take one element from enumerable of {element, weight} or from structure returned by create_searcher/1.