Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade to math/rand/v2 #483

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading