Shuffle List
Rearrange the values in a list in random order.
Examples in
Go
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
a := []int{1, 2, 3, 4, 5}
for i := len(a) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
fmt.Println(a)
// You can also use rand.Shuffle
b := []int{6, 7, 8, 9, 10}
rand.Shuffle(len(b), func(i, j int) {
b[i], b[j] = b[j], b[i]
})
fmt.Println(b)
}
Last Run
:
[2 4 3 1 5]
[10 6 8 7 9]