mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-02 08:27:56 +01:00
Remove math/rand and only use crypto/rand
This commit is contained in:
parent
195f39182d
commit
950b5dc1ce
3 changed files with 18 additions and 15 deletions
|
|
@ -1,24 +1,19 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"time"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
type WeightedChooser struct {
|
||||
entries []interface{}
|
||||
weights []int
|
||||
totalWeight int
|
||||
rng *rand.Rand
|
||||
}
|
||||
|
||||
func NewWeightedRandomChooser() *WeightedChooser {
|
||||
src := rand.NewSource(time.Now().UTC().UnixNano())
|
||||
|
||||
return &WeightedChooser{
|
||||
rng: rand.New(src), // nolint:gosec
|
||||
}
|
||||
return &WeightedChooser{}
|
||||
}
|
||||
|
||||
func (w *WeightedChooser) Put(value interface{}, weight int) {
|
||||
|
|
@ -43,9 +38,14 @@ func (w *WeightedChooser) GetAndRemove() (interface{}, error) {
|
|||
|
||||
// Based on https://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/
|
||||
func (w *WeightedChooser) weightedChoice() (int, error) {
|
||||
rnd := w.rng.Intn(w.totalWeight)
|
||||
if w.totalWeight == 0 {
|
||||
return 0, errors.New("no choices available")
|
||||
}
|
||||
rndBig, _ := rand.Int(rand.Reader, big.NewInt(int64(w.totalWeight)))
|
||||
|
||||
rnd := rndBig.Int64()
|
||||
for i, weight := range w.weights {
|
||||
rnd -= weight
|
||||
rnd -= int64(weight)
|
||||
if rnd < 0 {
|
||||
return i, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue