marcelom.github.io

Go Slice Shuffle

Posted on 07 Jun 2013 by Marcelo Moreira — Permalink

I am porting some Python code to Go, and I needed to perform a in-place slice (array) shuffle. I was surprised to discover that there is none !!!

Basically, I wanted something like this in Python:

a = range(1,11)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> random.shuffle(a)
>>> a
[8, 9, 2, 1, 4, 10, 7, 5, 3, 6]

I then came across this post.

Basically, I implemented it using Sattolo’s variant of the Fisher–Yates shuffle algorythm.

The code is more or less like this:

package main

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

func Shuffle(a []int) {
    for i := range a {
        j := rand.Intn(i + 1)
        a[i], a[j] = a[j], a[i]
    }
}

func main() {
    a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    rand.Seed(time.Now().UnixNano())
    Shuffle(a)
    fmt.Println(a)
}

Here is a sample output:

$ go run myshuffle.go
[7 10 8 2 5 1 4 6 9 3]

And here is the Gist and a Golang Payground.

Fork me on GitHub