Welcome To Golang By Example

Shuffle a slice or array in Go (Golang)

Table of Contents

Overview

math/rand package of go provides a Shuffle method that can be used shuffle an array or a slice. This method pseudo-randomizes the order of elements using the default source. pseudo-randomizes means that for a fixed input seed it will generate the same randomization. That is why in our program we will initialize the rand package with a different seed every time.

Below is the signature of the function.

func Shuffle(n int, swap func(i, j int))

This function takes in arguments

Also note that this function will panic if n<0. Let’s look at the code.

Code:

package main

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

func main() {
    rand.Seed(time.Now().Unix())

    in := []int{2, 3, 5, 8}
    rand.Shuffle(len(in), func(i, j int) {
        in[i], in[j] = in[j], in[i]
    })
    fmt.Println(in)

    rand.Shuffle(len(in), func(i, j int) {
        in[i], in[j] = in[j], in[i]
    })
    fmt.Println(in)
}

Output:

It can produce a different output on your machine.

[5 3 2 8]
[3 5 8 2]