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
+14.6k Golang : How to get URL port?
+8.8k Yum Error: no such table: packages
+10.8k Golang : Natural string sorting example
+37.7k Golang : Comparing date or timestamp
+14.5k Golang : Find network of an IP address
+7.7k Golang : Mapping Iban to Dunging alphabets
+5.3k Golang : How to deal with configuration data?
+12.5k Golang : "https://" not allowed in import path
+6.6k Golang : Embedded or data bundling example
+10.8k Android Studio : Checkbox for user to select options example
+8.6k Golang : Add text to image and get OpenCV's X, Y co-ordinates example