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
+11.6k Golang : How to detect a server/machine network interface capabilities?
+32.1k Golang : Convert []string to []byte examples
+5.3k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+4.6k Chrome : How to block socketloop.com links in Google SERP?
+6.9k Golang : How to call function inside template with template.FuncMap
+8.3k Your page has meta tags in the body instead of the head
+20.1k Golang : Determine if directory is empty with os.File.Readdir() function
+10.5k Golang : ISO8601 Duration Parser example
+11.8k Golang : How to parse plain email text and process email header?
+36.3k Golang : Convert date or time stamp from string to time.Time type
+29.3k Golang : How to create new XML file ?