Simplify random.Int64 usage with generics
This commit is contained in:
@@ -3,9 +3,11 @@ package random
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
func Int64(max int64) int64 {
|
||||
rnd, _ := rand.Int(rand.Reader, big.NewInt(max))
|
||||
func Int64[T constraints.Integer](max T) int64 {
|
||||
rnd, _ := rand.Int(rand.Reader, big.NewInt(int64(max)))
|
||||
return rnd.Int64()
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ func (w *WeightedChooser) weightedChoice() (int, error) {
|
||||
if w.totalWeight == 0 {
|
||||
return 0, errors.New("no choices available")
|
||||
}
|
||||
rnd := Int64(int64(w.totalWeight))
|
||||
rnd := Int64(w.totalWeight)
|
||||
for i, weight := range w.weights {
|
||||
rnd -= int64(weight)
|
||||
if rnd < 0 {
|
||||
|
||||
Reference in New Issue
Block a user