weightedrand

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2020 License: MIT Imports: 2 Imported by: 90

README ยถ

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}, // alternatively: wr.NewChoice('๐Ÿ†', 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/

Documentation ยถ

Overview ยถ

Package weightedrand contains a performant data structure and algorithm used to randomly select an element from some kind of list, where the chances of each element to be selected not being equal, but defined by relative "weights" (or probabilities). This is called weighted random selection.

Compare this package with (github.com/jmcvetta/randutil).WeightedChoice, which is optimized for the single operation case. In contrast, this package creates a presorted cache optimized for binary search, allowing for repeated selections from the same set to be significantly faster, especially for large data sets.

Example ยถ

In this example, we create a Chooser to pick from amongst various emoji fruit runes. We assign a numeric weight to each choice. These weights are relative, not on any absolute scoring system. In this trivial case, we will assign a weight of 0 to all but one fruit, so that the output will be predictable.

chooser := NewChooser(
	NewChoice('๐Ÿ‹', 0),
	NewChoice('๐ŸŠ', 0),
	NewChoice('๐Ÿ‰', 0),
	NewChoice('๐Ÿฅ‘', 42),
)
fruit := chooser.Pick().(rune)
fmt.Printf("%c", fruit)
Output:

๐Ÿฅ‘

Index ยถ

Examples ยถ

Constants ยถ

This section is empty.

Variables ยถ

This section is empty.

Functions ยถ

This section is empty.

Types ยถ

type Choice ยถ

type Choice struct {
	Item   interface{}
	Weight uint
}

Choice is a generic wrapper that can be used to add weights for any item.

func NewChoice ยถ added in v0.2.2

func NewChoice(item interface{}, weight uint) Choice

NewChoice creates a new Choice with specified item and weight.

type Chooser ยถ

type Chooser struct {
	// contains filtered or unexported fields
}

A Chooser caches many possible Choices in a structure designed to improve performance on repeated calls for weighted random selection.

func NewChooser ยถ

func NewChooser(cs ...Choice) Chooser

NewChooser initializes a new Chooser for picking from the provided Choices.

func (Chooser) Pick ยถ

func (chs Chooser) Pick() interface{}

Pick returns a single weighted random Choice.Item from the Chooser.

Directories ยถ

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL