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
+14k Golang : Find location by IP address and display with Google Map
+6.3k Golang : Trim everything onward after a word
+5.2k PageSpeed : Clear or flush cache on web server
+5.6k Golang : How to determine if request or crawl is from Google robots
+11.8k Generate salted password with OpenSSL example
+6.3k Golang : How to handle file size larger than available memory panic issue
+2.3k Golang : Switch Redis database redis.NewClient
+73.1k Golang : How to return HTTP status code?
+8k Golang : Go as a script or running go with shebang/hashbang style
+4.7k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+10.9k Golang : Convert a rune to unicode style string \u