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
+7k Golang : Levenshtein distance example
+5k Golang : Calculate a pip value and distance to target profit example
+31.6k Golang : Get local IP and MAC address
+16.9k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+17.5k Golang : Clone with pointer and modify value
+17.7k How to enable MariaDB/MySQL logs ?
+13.6k Golang : reCAPTCHA example
+8.2k Golang : HttpRouter multiplexer routing example
+6.6k Golang : Totalize or add-up an array or slice example
+10.5k Generate Random number with math/rand in Go
+8.8k Golang : Take screen shot of browser with JQuery example
+7.8k Golang : Scan files for certain pattern and rename part of the files