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
+8.3k Golang : Configure Apache and NGINX to access your Go service example
+7.8k Golang : Scan files for certain pattern and rename part of the files
+10.5k Swift : Convert (cast) String to Integer
+18.5k Golang : Set, Get and List environment variables
+11.9k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+14k Golang : concatenate(combine) strings
+14.8k Golang : Get URI segments by number and assign as variable example
+6.9k Golang : Calculate BMI and risk category
+10.6k Golang : ISO8601 Duration Parser example
+10.3k Golang : How to check if a website is served via HTTPS
+12.7k Golang : Remove or trim extra comma from CSV
+30.9k Golang : Interpolating or substituting variables in string examples