Skip to content

jdiscar/vose-alias-method.js

Repository files navigation

VoseAliasMethod.js

Original Algorithm: Keith Schwarz (htiek@cs.stanford.edu)
Ported to Javascript by: JDiscar (j.discar+github@gmail.com)

TL;DR

Simulates rolling an n-sided loaded dice.

Full Description

An implementation of the alias method implemented using Vose's algorithm. The alias method allows for efficient sampling of random values from a discrete probability distribution in O(1) time each, after a one-time O(n) preprocessing time.

For a complete writeup on the alias method, including the intuition and important proofs, please see the article "Darts, Dice, and Coins- Sampling from a Discrete Distribution" at: http://www.keithschwarz.com/darts-dice-coins/

Supports Node.js, CommonJS, AMD, or Global use.

I realize that this project is overkill for what could have been provided in a simple gist, but I was using this project to experiment with javascript package management/builds.

Sample Use

var generator = new VoseAliasMethod([.1, .2, .3, .4]);  
var chosenIndex1 = generator.next();  
var chosenIndex2 = generator.next();  

Using NPM

  • Install the current stable version of Node.JS
  • Run npm install within the project directory.
  • Run npm install mocha -g within the project directory to install mocha for testing.
  • Run npm install uglify-js -g within the project directory to install uglifyjs for minimizing.
  • Run npm install jsl -g within the project directory to install jsl.
  • Run npm install jshint -g within the project directory to install jshint.
  • Run npm run-script build to create vose-alias-method-min.js.
  • Run npm test to run the mocha tests in the test directory.
  • All tests should pass, if they don't:
    • try to fix any obvious dependency issues, do you have make installed?
    • is the problem my fault, create an issue on this project.

About

Vose Alias Method javascript module

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published