All Projects → mroth → Weightedrand

mroth / Weightedrand

Licence: mit
⚖️ Fast weighted random selection for Go

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Labels

Projects that are alternatives of or similar to Weightedrand

Rasusa
Randomly subsample sequencing reads to a specified coverage
Stars: ✭ 28 (-70.83%)
Mutual labels:  random
Kirby3 Autoid
Automatic unique ID for Pages, Files and Structures including performant helpers to retrieve them. Bonus: Tiny-URL.
Stars: ✭ 58 (-39.58%)
Mutual labels:  random
Spicy Proton
Generate a random English adjective-noun word pair in Ruby
Stars: ✭ 76 (-20.83%)
Mutual labels:  random
Minecraft Randomiser
Resource pack and data pack randomiser to shuffle textures, sounds, loot tables, recipes, and more
Stars: ✭ 34 (-64.58%)
Mutual labels:  random
Random compat
PHP 5.x support for random_bytes() and random_int()
Stars: ✭ 7,950 (+8181.25%)
Mutual labels:  random
Pgen
Command-line passphrase generator
Stars: ✭ 68 (-29.17%)
Mutual labels:  random
Random Password Generator
Automatic random password generator class for PHP
Stars: ✭ 9 (-90.62%)
Mutual labels:  random
Randomcolor
Javascript module for generating random, distinguishable, pleasing colors (ie: for chart series).
Stars: ✭ 83 (-13.54%)
Mutual labels:  random
Yesterday I Learned
Brainfarts are caused by the rupturing of the cerebral sphincter.
Stars: ✭ 50 (-47.92%)
Mutual labels:  random
Random Word
This is a simple python package to generate random english words
Stars: ✭ 75 (-21.87%)
Mutual labels:  random
Wyhash Rs
wyhash fast portable non-cryptographic hashing algorithm and random number generator in Rust
Stars: ✭ 44 (-54.17%)
Mutual labels:  random
Troschuetz.random
Fully managed library providing various random number generators and distributions.
Stars: ✭ 47 (-51.04%)
Mutual labels:  random
Random
Generate random strings or numeric values
Stars: ✭ 68 (-29.17%)
Mutual labels:  random
Mir Random
Advanced Random Number Generators
Stars: ✭ 30 (-68.75%)
Mutual labels:  random
Eth Random
commit-reveal RNG method in Ethereum
Stars: ✭ 79 (-17.71%)
Mutual labels:  random
Session Token
Secure, efficient, simple random session token generation
Stars: ✭ 12 (-87.5%)
Mutual labels:  random
Easy Random
The simple, stupid random Java beans/records generator
Stars: ✭ 1,095 (+1040.63%)
Mutual labels:  random
Keygen Php
A fluent PHP random key generator.
Stars: ✭ 93 (-3.12%)
Mutual labels:  random
Birdseed
🐦 🎲 Use Twitter's Search API to get random numbers
Stars: ✭ 81 (-15.62%)
Mutual labels:  random
Random Bytes Readable Stream
Creates a readable stream producing cryptographically strong pseudo-random data using `crypto.randomBytes()`
Stars: ✭ 71 (-26.04%)
Mutual labels:  random

weightedrand ⚖️

PkgGoDev CodeFactor Build Status codecov

Fast weighted random selection for Go.

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

Usage

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

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

    chooser, _ := 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 := chooser.Pick().(string)
    fmt.Println(result)
}

Performance

The existing Go library that has a comparable 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.

Comparison of this library versus randutil.ChooseWeighted on my workstation. For repeated samplings from large collections, weightedrand will be much quicker:

Num choices randutil weightedrand weightedrand -cpu=8*
10 201 ns/op 38 ns/op 2.9 ns/op
100 267 ns/op 51 ns/op 4.1 ns/op
1,000 1012 ns/op 67 ns/op 5.4 ns/op
10,000 8683 ns/op 83 ns/op 6.9 ns/op
100,000 123500 ns/op 105 ns/op 12.0 ns/op
1,000,000 2399614 ns/op 218 ns/op 17.2 ns/op
10,000,000 26804440 ns/op 432 ns/op 35.1 ns/op

*: Since v0.3.0 weightedrand can efficiently utilize a single Chooser across multiple CPU cores in parallel, making it even faster in overall throughput. See PR#2 for details. Informal benchmarks conducted on an Intel Xeon W-2140B CPU (8 core @ 3.2GHz, hyperthreading enabled).

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 initialization time and memory storage.

Caveats

Note this library utilizes math/rand instead of crypto/rand, as it is optimized for performance, and is not intended to be used for cryptographically secure requirements.

Credits

To better understand the algorithm used in this library (as well as the one used in randutil) check out this great blog post: Weighted random generation in Python.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].