Skip to content

Sample arbitrary discrete probability distributions (super duper fast)

Notifications You must be signed in to change notification settings

wbrickner/disc-dist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DiscDist (Discrete Distribution)

DiscDist accepts an array of numbers, and returns a random index from that array, with a probability distribution which matches the elements in the array. DiscDist is highly performant (when used correctly).

For example:

[ 1, 2, 1 ] will have: 

  a 25% chance of returning 0, 
  a 50% chance of returning 1,
  a 25% chance of returning 2

Installation

$ npm i disc-dist

Usage

One-off usage

Only use this approach if you know you only plan to query the distribution one time.

const discDist = require("disc-dist")

let randomIndex = discDist.randomSingleUse(dist)

Repeated usage

Use this approach if it's possible that you will query the distribution more than one time.

This function has many optimizations which are only appropriate for repeated use, and which can provide substantial performance improvements.

const discDist = require("disc-dist")
let multiuse = discDist.randomMultiUse(dist)

// then, as many times as you want:
let randomIndex = multiuse.random()

Complexity

  • DiscDist runs in O(n) in the worst case.

Because of the random nature of the algorithm, the average case can be much better depending on your distribution.

Distributions are auto-optimized to provide the fastest execution time.

The performance data below was measured on my 2018 Macbook Pro: MacBook Pro (13-inch, 2018, Four Thunderbolt 3 Ports), 2.7 GHz Intel Core i7.

You can measure your own performance numbers by running: node ./benchmarks/.

Distribution Elements Exection Time Ops / Sec
10 207ns 4,819,678
100 222ns 4,502,697
1,000 0.578us 1,727,480
10,000 3.895us 256,733
100,000 36.63us 27,295
1,000,000 0.412ms 2427
10,000,000 3.947ms 253

A plot of the above data:

Execution Time Plot

About

Sample arbitrary discrete probability distributions (super duper fast)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published