Skip to content

Commit

Permalink
Merge pull request #22 from life4/no-global-rand-seed
Browse files Browse the repository at this point in the history
Don't use global rand.Seed
  • Loading branch information
orsinium authored Aug 24, 2023
2 parents 3a75f8c + 897d6cc commit 14c724f
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions slices/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func Choice[S ~[]T, T any](items S, seed int64) (T, error) {
if seed == 0 {
seed = time.Now().UnixNano()
}
rand.Seed(seed)
i := rand.Intn(len(items))
r := rand.New(rand.NewSource(seed))
i := r.Intn(len(items))
return items[i], nil
}

Expand Down Expand Up @@ -448,11 +448,11 @@ func Shuffle[S ~[]T, T any](items S, seed int64) {
if seed == 0 {
seed = time.Now().UnixNano()
}
rand.Seed(seed)
r := rand.New(rand.NewSource(seed))
swap := func(i, j int) {
items[i], items[j] = items[j], items[i]
}
rand.Shuffle(len(items), swap)
r.Shuffle(len(items), swap)
}

// Sort returns sorted slice
Expand Down Expand Up @@ -548,11 +548,11 @@ func TakeRandom[S ~[]T, T any](items S, count int, seed int64) (S, error) {
if seed == 0 {
seed = time.Now().UnixNano()
}
rand.Seed(seed)
r := rand.New(rand.NewSource(seed))
swap := func(i, j int) {
items[i], items[j] = items[j], items[i]
}
rand.Shuffle(len(items), swap)
r.Shuffle(len(items), swap)
return items[:count], nil
}

Expand Down

0 comments on commit 14c724f

Please sign in to comment.