Skip to content

A C++ implementation of Walker's Alias Method for quickly sampling from an array with a given probability distribution

Notifications You must be signed in to change notification settings

m-ochi/aliasmethod

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

WalkerMethod

Walker's Alias Method is an O(1) algorithm for selecting elements from an array given a weighted distribution.

For example, let's say you want to return :win 60% of the time, :lose 20% of the time, and :tie 20% of the time. You could fill an array with 6 examples of :win and 2 examples each of :lose and :tie, then pick a random element. You could also use ranges, picking a random number between 0.0 and 1.0 and returning :win when the number is below 0.6, :lose if the number is below 0.8, and :tie otherwise. But, these algorithms are still O(n). You can do better by using a heap or binary search tree, but Walker's Alias Method is better still, with a constant runtime once an O(n) pre-computation phase has completed.

how to use

  • if you need main function, please comment out there.
  • cmake .
  • make

About

A C++ implementation of Walker's Alias Method for quickly sampling from an array with a given probability distribution

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages