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
+12.1k Golang : convert(cast) string to integer value
+21.8k Golang : How to reverse slice or array elements order
+8.2k Golang : Add build version and other information in executables
+5.4k Golang : Return multiple values from function
+11.1k Golang : Read until certain character to break for loop
+9.2k Golang : How to check if a string with spaces in between is numeric?
+9.1k Golang : Intercept and compare HTTP response code example
+4.6k Mac OSX : Get disk partitions' size, type and name
+30.4k Golang : How to redirect to new page with net/http?
+6k Golang : How to verify input is rune?
+39k Golang : How to iterate over a []string(array)
+5.2k PHP : See installed compiled-in-modules