Skip to content

Commit

Permalink
feat: upgrade to math/rand/v2 (#483)
Browse files Browse the repository at this point in the history
* feat: upgrade to math/rand/v2

* lint: silent warning
  • Loading branch information
samber authored Jun 30, 2024
1 parent 0a145c7 commit 3b29b7d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
11 changes: 4 additions & 7 deletions channel.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package lo

import (
"math/rand"
"sync"
"time"

"github.com/samber/lo/internal/rand"
)

type DispatchingStrategy[T any] func(msg T, index uint64, channels []<-chan T) int
Expand Down Expand Up @@ -86,9 +87,7 @@ func DispatchingStrategyRoundRobin[T any](msg T, index uint64, channels []<-chan
// If the channel capacity is exceeded, another random channel will be selected and so on.
func DispatchingStrategyRandom[T any](msg T, index uint64, channels []<-chan T) int {
for {
// @TODO: Upgrade to math/rand/v2 as soon as we set the minimum Go version to 1.22.
// bearer:disable go_gosec_crypto_weak_random
i := rand.Intn(len(channels))
i := rand.IntN(len(channels))
if channelIsNotFull(channels[i]) {
return i
}
Expand All @@ -110,9 +109,7 @@ func DispatchingStrategyWeightedRandom[T any](weights []int) DispatchingStrategy

return func(msg T, index uint64, channels []<-chan T) int {
for {
// @TODO: Upgrade to math/rand/v2 as soon as we set the minimum Go version to 1.22.
// bearer:disable go_gosec_crypto_weak_random
i := seq[rand.Intn(len(seq))]
i := seq[rand.IntN(len(seq))]
if channelIsNotFull(channels[i]) {
return i
}
Expand Down
10 changes: 3 additions & 7 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package lo

import (
"fmt"
"math/rand"
"time"

"github.com/samber/lo/internal/constraints"
"github.com/samber/lo/internal/rand"
)

// IndexOf returns the index at which the first occurrence of a value is found in an array or return -1
Expand Down Expand Up @@ -432,9 +432,7 @@ func Sample[T any](collection []T) T {
return Empty[T]()
}

// @TODO: Upgrade to math/rand/v2 as soon as we set the minimum Go version to 1.22.
// bearer:disable go_gosec_crypto_weak_random
return collection[rand.Intn(size)]
return collection[rand.IntN(size)]
}

// Samples returns N random unique items from collection.
Expand All @@ -448,9 +446,7 @@ func Samples[T any, Slice ~[]T](collection Slice, count int) Slice {
for i := 0; i < size && i < count; i++ {
copyLength := size - i

// @TODO: Upgrade to math/rand/v2 as soon as we set the minimum Go version to 1.22.
// bearer:disable go_gosec_crypto_weak_random
index := rand.Intn(size - i)
index := rand.IntN(size - i)
results = append(results, copy[index])

// Removes element.
Expand Down
14 changes: 14 additions & 0 deletions internal/rand/ordered_go118.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !go1.22

package rand

import "math/rand"

func Shuffle(n int, swap func(i, j int)) {
rand.Shuffle(n, swap)
}

func IntN(n int) int {
// bearer:disable go_gosec_crypto_weak_random
return rand.Intn(n)
}
13 changes: 13 additions & 0 deletions internal/rand/ordered_go122.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build go1.22

package rand

import "math/rand/v2"

func Shuffle(n int, swap func(i, j int)) {
rand.Shuffle(n, swap)
}

func IntN(n int) int {
return rand.IntN(n)
}
2 changes: 1 addition & 1 deletion slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package lo

import (
"sort"
"math/rand"

"github.com/samber/lo/internal/constraints"
"github.com/samber/lo/internal/rand"
)

// Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.
Expand Down
7 changes: 3 additions & 4 deletions string.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package lo

import (
"math/rand"
"regexp"
"strings"
"unicode"
"unicode/utf8"

"github.com/samber/lo/internal/rand"

"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -39,9 +40,7 @@ func RandomString(size int, charset []rune) string {
b := make([]rune, size)
possibleCharactersCount := len(charset)
for i := range b {
// @TODO: Upgrade to math/rand/v2 as soon as we set the minimum Go version to 1.22.
// bearer:disable go_gosec_crypto_weak_random
b[i] = charset[rand.Intn(possibleCharactersCount)]
b[i] = charset[rand.IntN(possibleCharactersCount)]
}
return string(b)
}
Expand Down

0 comments on commit 3b29b7d

Please sign in to comment.