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
+12.6k Javascript : Prompt confirmation before exit
+14.1k Golang : Validate hostname
+13.6k Golang : Search folders for file recursively with wildcard support
+6.7k Golang : Ways to recover memory during run time.
+17.6k Golang : Populate dropdown with html/template example
+6.3k Golang : Dealing with struct's private part
+9.1k Golang : Wait and sync.WaitGroup example
+42.6k Golang : Use wildcard patterns with filepath.Glob() example
+28.3k Golang : Get and Set User-Agent examples
+5.8k How to let Facebook Login button redirect to a particular URL ?
+19.2k Golang : Underscore or snake_case to camel case example
+6.9k Golang : Configure Apache and NGINX to access your Go service example