GithubHelp home page GithubHelp logo

yrh79 / weightedrand Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mroth/weightedrand

0.0 1.0 0.0 15 KB

:balance_scale: Fast weighted random selection for Go

License: MIT License

Go 100.00%

weightedrand's Introduction

weightedrand

Build Status Go Report Card GoDoc

Randomly select an element from some kind of list, with the chances of each element to be selected not being equal, but defined by relative "weights" (or probabilities). This is called weighted random selection.

The existing Go library that has a generic implementation of this is github.com/jmcvetta/randutil, which optimizes for the single operation case. In contrast, this library creates a presorted cache optimized for binary search, allowing repeated selections from the same set to be significantly faster, especially for large data sets.

Usage

import (
    /* ...snip... */
    wr "github.com/mroth/weightedrand"
)

func main() {
    rand.Seed(time.Now().UTC().UnixNano()) // always seed random!

    c := wr.NewChooser(
        wr.Choice{Item: "๐Ÿ†", Weight: 0},
        wr.Choice{Item: "๐Ÿ‹", Weight: 1},
        wr.Choice{Item: "๐ŸŠ", Weight: 1},
        wr.Choice{Item: "๐Ÿ‰", Weight: 3},
        wr.Choice{Item: "๐Ÿฅ‘", Weight: 5},
    )
    /* The following will print ๐Ÿ‹ and ๐ŸŠ with 0.1 probability, ๐Ÿ‰ with 0.3
    probability, and ๐Ÿฅ‘ with 0.5 probability. ๐Ÿ† will never be printed. (Note
    the weights don't have to add up to 10, that was just done here to make the
    example easier to read.) */
    result := c.Pick().(string)
    fmt.Println(result)
}

Benchmarks

Comparison of this library versus randutil.ChooseWeighted. For large numbers of samplings from large collections, weightedrand will be quicker.

Num choices randutil weightedrand
10 435 ns/op 58 ns/op
100 511 ns/op 84 ns/op
1,000 1297 ns/op 112 ns/op
10,000 7952 ns/op 137 ns/op
100,000 85142 ns/op 173 ns/op
1,000,000 2082248 ns/op 312 ns/op

Don't be mislead by these numbers into thinking weightedrand is always the right choice! If you are only picking from the same distribution once, randutil will be faster. weightedrand optimizes for repeated calls at the expense of some setup time and memory storage.

Caveats

Note this uses math/rand instead of crypto/rand, as it is optimized for performance, not cryptographically secure implementation.

Relies on global rand for determinism, therefore, don't forget to seed random!

Credits

The algorithm used in this library (as well as the one used in randutil) comes from: https://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/

weightedrand's People

Contributors

mroth avatar yrh79 avatar

Watchers

James Cloos 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.