Golang : Generate random elements without repetition or duplicate
Problem :
How to generate a map with random elements without repetition or duplicate items in the map?
Solution :
Use the rand.Perm
function to generate a map with random elements.
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println(rand.Perm(16)[:16])
}
Output :
[12 4 2 13 10 0 3 11 7 5 15 1 9 14 6 8]
See also : Golang : Check if element exist in map
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.2k Golang : Inject/embed Javascript before sending out to browser example
+26.9k Golang : How to check if a connection to database is still alive ?
+4.9k JavaScript: Add marker function on Google Map
+12.5k Golang : 2 dimensional array example
+8.7k Golang : Generate Datamatrix barcode
+7k Get Facebook friends working in same company
+10.4k Golang : Check a web page existence with HEAD request example
+14.6k Golang : Parsing or breaking down URL
+9.7k Golang : Convert(cast) string to int64
+12.5k Golang : Print UTF-8 fonts on image example
+7.1k Default cipher that OpenSSL used to encrypt a PEM file
+3.9k Java : Random alphabets, alpha-numeric or numbers only string generator