4
4
[ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/mroth/weightedrand )] ( https://goreportcard.com/report/github.com/mroth/weightedrand )
5
5
[ ![ GoDoc] ( https://godoc.org/github.com/mroth/weightedrand?status.svg )] ( https://godoc.org/github.com/mroth/weightedrand )
6
6
7
- Randomly select an element from some kind of list, with the chances of each
8
- element to be selected not being equal, but defined by relative "weights" (or
9
- probabilities). This is called weighted random selection.
7
+ > Fast weighted random selection for Go.
10
8
11
- The existing Go library that has a generic implementation of this is
12
- [ ` github.com/jmcvetta/randutil ` ] [ 1 ] , which optimizes for the single operation
13
- case. In contrast, this library creates a presorted cache optimized for binary
14
- search, allowing repeated selections from the same set to be significantly
15
- faster, especially for large data sets.
16
-
17
- [ 1 ] : https://github.com/jmcvetta/randutil
9
+ Randomly selects an element from some kind of list, where the chances of each
10
+ element to be selected are not equal, but rather defined by relative "weights"
11
+ (or probabilities). This is called weighted random selection.
18
12
19
13
## Usage
20
14
@@ -44,8 +38,17 @@ func main() {
44
38
```
45
39
46
40
## Benchmarks
47
- Comparison of this library versus ` randutil.ChooseWeighted ` . For large numbers
48
- of samplings from large collections, ` weightedrand ` will be quicker.
41
+
42
+ The existing Go library that has a comparable implementation of this is
43
+ [ ` github.com/jmcvetta/randutil ` ] [ 1 ] , which optimizes for the single operation
44
+ case. In contrast, this library creates a presorted cache optimized for binary
45
+ search, allowing repeated selections from the same set to be significantly
46
+ faster, especially for large data sets.
47
+
48
+ [ 1 ] : https://github.com/jmcvetta/randutil
49
+
50
+ Comparison of this library versus ` randutil.ChooseWeighted ` . For repeated
51
+ samplings from large collections, ` weightedrand ` will be much quicker.
49
52
50
53
| Num choices | ` randutil ` | ` weightedrand ` |
51
54
| ----------: | ------------: | -------------: |
@@ -61,15 +64,16 @@ right choice! If you are only picking from the same distribution once,
61
64
` randutil ` will be faster. ` weightedrand ` optimizes for repeated calls at the
62
65
expense of some setup time and memory storage.
63
66
67
+ * Update: Starting in ` v0.3.0 ` weightedrand can now scale linearly to take
68
+ advantage of multiple CPU cores in parallel, making it even faster. See
69
+ [ PR #2 ] ( https://github.com/mroth/weightedrand/pull/2 ) for details.*
70
+
64
71
## Caveats
65
72
66
73
Note this uses ` math/rand ` instead of ` crypto/rand ` , as it is optimized for
67
- performance, not cryptographically secure implementation.
68
-
69
- Relies on global rand for determinism, therefore, don't forget to seed random!
74
+ performance, not a cryptographically secure implementation.
70
75
71
76
## Credits
72
77
73
- The algorithm used in this library (as well as the one used in randutil) comes
74
- from:
75
- https://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/
78
+ To better understand the algorithm used in this library (as well as the one used
79
+ in randutil) check out this great blog post: [ Weighted random generation in Python] ( https://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/ ) .
0 commit comments