Created
May 4, 2015 22:55
-
-
Save yiting007/043701b6c36fa97d0192 to your computer and use it in GitHub Desktop.
Shuffle an array in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
func main() { | |
array := []int{1, 2, 3, 4, 5, 6, 7} | |
random(array) | |
} | |
func random(array []int) { | |
rand.Seed(time.Now().UTC().UnixNano()) | |
for i := len(array) - 1; i > 0; i-- { | |
j := rand.Intn(i + 1) | |
array[i], array[j] = array[j], array[i] | |
} | |
fmt.Println(array) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment