update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
This commit is contained in:
24
utils/random/number.go
Normal file
24
utils/random/number.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package random
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"math/big"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
// Int64N returns a random int64 between 0 and max.
|
||||
// This is a reimplementation of math/rand/v2.Int64N using a cryptographically secure random number generator.
|
||||
func Int64N[T constraints.Integer](max T) int64 {
|
||||
rnd, _ := rand.Int(rand.Reader, big.NewInt(int64(max)))
|
||||
return rnd.Int64()
|
||||
}
|
||||
|
||||
// Uint64 returns a random uint64.
|
||||
// This is a reimplementation of math/rand/v2.Uint64 using a cryptographically secure random number generator.
|
||||
func Uint64() uint64 {
|
||||
buffer := make([]byte, 8)
|
||||
_, _ = rand.Read(buffer)
|
||||
return binary.BigEndian.Uint64(buffer)
|
||||
}
|
||||
24
utils/random/number_test.go
Normal file
24
utils/random/number_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package random_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/navidrome/navidrome/utils/random"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestRandom(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Random Suite")
|
||||
}
|
||||
|
||||
var _ = Describe("number package", func() {
|
||||
Describe("Int64N", func() {
|
||||
It("should return a random int64", func() {
|
||||
for i := 0; i < 10000; i++ {
|
||||
Expect(random.Int64N(100)).To(BeNumerically("<", 100))
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
70
utils/random/weighted_random_chooser.go
Normal file
70
utils/random/weighted_random_chooser.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package random
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// WeightedChooser allows to randomly choose an entry based on their weights
|
||||
// (higher weight = higher chance of being chosen). Based on the subtraction method described in
|
||||
// https://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/
|
||||
type WeightedChooser[T any] struct {
|
||||
entries []T
|
||||
weights []int
|
||||
totalWeight int
|
||||
}
|
||||
|
||||
func NewWeightedChooser[T any]() *WeightedChooser[T] {
|
||||
return &WeightedChooser[T]{}
|
||||
}
|
||||
|
||||
func (w *WeightedChooser[T]) Add(value T, weight int) {
|
||||
w.entries = append(w.entries, value)
|
||||
w.weights = append(w.weights, weight)
|
||||
w.totalWeight += weight
|
||||
}
|
||||
|
||||
// Pick choose a random entry based on their weights, and removes it from the list
|
||||
func (w *WeightedChooser[T]) Pick() (T, error) {
|
||||
var empty T
|
||||
if w.totalWeight == 0 {
|
||||
return empty, errors.New("cannot choose from zero weight")
|
||||
}
|
||||
i, err := w.weightedChoice()
|
||||
if err != nil {
|
||||
return empty, err
|
||||
}
|
||||
entry := w.entries[i]
|
||||
_ = w.Remove(i)
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (w *WeightedChooser[T]) weightedChoice() (int, error) {
|
||||
if len(w.entries) == 0 {
|
||||
return 0, errors.New("cannot choose from empty list")
|
||||
}
|
||||
rnd := Int64N(w.totalWeight)
|
||||
for i, weight := range w.weights {
|
||||
rnd -= int64(weight)
|
||||
if rnd < 0 {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
return 0, errors.New("internal error - code should not reach this point")
|
||||
}
|
||||
|
||||
func (w *WeightedChooser[T]) Remove(i int) error {
|
||||
if i < 0 || i >= len(w.entries) {
|
||||
return errors.New("index out of bounds")
|
||||
}
|
||||
|
||||
w.totalWeight -= w.weights[i]
|
||||
|
||||
w.weights = slices.Delete(w.weights, i, i+1)
|
||||
w.entries = slices.Delete(w.entries, i, i+1)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *WeightedChooser[T]) Size() int {
|
||||
return len(w.entries)
|
||||
}
|
||||
72
utils/random/weighted_random_chooser_test.go
Normal file
72
utils/random/weighted_random_chooser_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package random
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("WeightedChooser", func() {
|
||||
var w *WeightedChooser[int]
|
||||
BeforeEach(func() {
|
||||
w = NewWeightedChooser[int]()
|
||||
for i := 0; i < 10; i++ {
|
||||
w.Add(i, i+1)
|
||||
}
|
||||
})
|
||||
|
||||
It("selects and removes a random item", func() {
|
||||
Expect(w.Size()).To(Equal(10))
|
||||
_, err := w.Pick()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(w.Size()).To(Equal(9))
|
||||
})
|
||||
|
||||
It("removes items", func() {
|
||||
Expect(w.Size()).To(Equal(10))
|
||||
for i := 0; i < 10; i++ {
|
||||
Expect(w.Remove(0)).To(Succeed())
|
||||
}
|
||||
Expect(w.Size()).To(Equal(0))
|
||||
})
|
||||
|
||||
It("returns error if trying to remove an invalid index", func() {
|
||||
Expect(w.Size()).To(Equal(10))
|
||||
Expect(w.Remove(-1)).ToNot(Succeed())
|
||||
Expect(w.Remove(10000)).ToNot(Succeed())
|
||||
Expect(w.Size()).To(Equal(10))
|
||||
})
|
||||
|
||||
It("returns the sole item", func() {
|
||||
ws := NewWeightedChooser[string]()
|
||||
ws.Add("a", 1)
|
||||
Expect(ws.Pick()).To(Equal("a"))
|
||||
})
|
||||
|
||||
It("returns all items from the list", func() {
|
||||
for i := 0; i < 10; i++ {
|
||||
Expect(w.Pick()).To(BeElementOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
|
||||
}
|
||||
Expect(w.Size()).To(Equal(0))
|
||||
})
|
||||
|
||||
It("fails when trying to choose from empty set", func() {
|
||||
w = NewWeightedChooser[int]()
|
||||
w.Add(1, 1)
|
||||
w.Add(2, 1)
|
||||
Expect(w.Pick()).To(BeElementOf(1, 2))
|
||||
Expect(w.Pick()).To(BeElementOf(1, 2))
|
||||
_, err := w.Pick()
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("chooses based on weights", func() {
|
||||
counts := [10]int{}
|
||||
for i := 0; i < 200000; i++ {
|
||||
c, _ := w.weightedChoice()
|
||||
counts[c] = counts[c] + 1
|
||||
}
|
||||
for i := 0; i < 9; i++ {
|
||||
Expect(counts[i]).To(BeNumerically("<", counts[i+1]))
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user