Giter Site home page Giter Site logo

weightedrandom's Introduction

minaguib/weightedrandom

A Go (golang) library for efficient weighted random picking

GoDoc Build Status Coverage Status GoReport

About

Given a theoretical jar of 1,000 marbles, like so:

Marble color Count
Red 500
Blue 250
Green 125
Yellow 120
Transparent 4
Vantablack 1

You want a solution that allows you to simulate picking a random marble while adhering to the above probabilities. You want it to operate as fast as possible, require as little storage as possible, and efficiently handle large weights and large number of items.

About the implementation

This library implements the Alias picking method as further optimized by Michael D. Vose

Ref. Paper: "A Linear Algorithm For Generating Random Numbers With a Given Distribution" by Michael D. Vose

Ref: Darts, Dice, and Coins: Sampling from a Discrete Distribution by Keith Schwarz

Performance characteristics

Item Cost
Time to initialize O(n)
Data structure storage O(n)
Time to pick an item O(1)

Performance benchmarks

On a MacBook Pro, Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz, 2133 MHz LPDDR3 memory:

$ go test -bench .
goos: darwin
goarch: amd64
pkg: github.com/minaguib/weightedrandom

BenchmarkNew/1_weights-8           126366      8967 ns/op
BenchmarkNew/10_weights-8          126315      9015 ns/op
BenchmarkNew/100_weights-8         112978      9985 ns/op
BenchmarkNew/1000_weights-8         58854     19891 ns/op
BenchmarkNew/10000_weights-8         9981    109303 ns/op
BenchmarkNew/100000_weights-8        1029   1001077 ns/op
BenchmarkNew/1000000_weights-8        112   9697761 ns/op
BenchmarkNew/10000000_weights-8        12  89310961 ns/op

BenchmarkPick/from_1_weights-8            76958226        13.8 ns/op
BenchmarkPick/from_10_weights-8           42556176        26.2 ns/op
BenchmarkPick/from_100_weights-8          43152367        26.0 ns/op
BenchmarkPick/from_1000_weights-8         42600124        26.0 ns/op
BenchmarkPick/from_10000_weights-8        39454466        27.1 ns/op
BenchmarkPick/from_100000_weights-8       31104279        32.5 ns/op
BenchmarkPick/from_1000000_weights-8      17665467        69.6 ns/op
BenchmarkPick/from_10000000_weights-8     11770742       104 ns/op

Usage

For usage, examples and documentation, see GoDoc

weightedrandom's People

Contributors

minaguib avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.