Golang math/rand.Seed() function example
package math/rand
Golang math/rand.Seed() function usage example
package main
import (
"fmt"
"math/rand"
"time"
)
func shuffle(arr []int) {
t := time.Now()
rand.Seed(int64(t.Nanosecond())) // no shuffling without Seed
for i := len(arr) - 1; i > 0; i-- {
j := rand.Intn(i)
arr[i], arr[j] = arr[j], arr[i]
}
}
func main() {
//list := rand.Perm(25)
list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
fmt.Printf("Original : %v\n", list)
shuffledList := list
shuffle(shuffledList)
fmt.Printf("Shuffled : %v\n", shuffledList)
}
References :
http://golang.org/pkg/math/rand/#Seed
https://www.socketloop.com/tutorials/golang-how-to-shuffle-elements-in-array-or-slice
Advertisement
Something interesting
Tutorials
+7.9k Javascript : Put image into Chrome browser's console
+14.9k Golang : Submit web forms without browser by http.PostForm example
+29.7k Golang : Record voice(audio) from microphone to .WAV file
+35.3k Golang : Strip slashes from string example
+6.1k Golang : Get missing location after unmarshal binary and gob decode time.
+9.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+19.2k Golang : Check if directory exist and create if does not exist
+12.7k Golang : Pass database connection to function called from another package and HTTP Handler
+7.5k Golang : Get YouTube playlist
+13k Golang : Calculate elapsed years or months since a date
+6.3k Golang : How to get capacity of a slice or array?
+8.9k Golang : Sort lines of text example