Files
rands/randsmust/shuffle.go
Jim Myhrberg a141938394 feat(shuffle)!: add Shuffle and ShuffleSlice functions (#11)
Add equivalent to `Shuffle` function from `math/rand` and `math/rand/v2`
packages, but based on randomness from `crypto/rand` package. This
allows cryptographically secure shuffling of data.

Also add `ShuffleSlice` function that shuffles a slice of any type.

BREAKING CHANGE: Minimum required Go version is now 1.18 due the `ShuffleSlice` using generics.
2025-03-10 23:50:25 +00:00

33 lines
952 B
Go

package randsmust
import "github.com/jimeh/rands"
// Shuffle randomizes the order of a collection of n elements using
// cryptographically secure random values from crypto/rand. It implements the
// Fisher-Yates shuffle algorithm.
//
// The swap function is called to exchange values at indices i and j. This
// signature is compatible with Shuffle from math/rand and math/rand/v2 for easy
// migration.
//
// If an error occurs during shuffling, this function will panic.
func Shuffle(n int, swap func(i, j int)) {
err := rands.Shuffle(n, swap)
if err != nil {
panic(err)
}
}
// ShuffleSlice randomizes the order of elements in a slice in-place using
// cryptographically secure random values from crypto/rand.
//
// It implements the Fisher-Yates shuffle algorithm.
//
// If an error occurs during shuffling, this function will panic.
func ShuffleSlice[T any](slice []T) {
err := rands.ShuffleSlice(slice)
if err != nil {
panic(err)
}
}