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
+35.2k Golang : Strip slashes from string example
+16.7k Golang : Read integer from file into array
+7.6k Golang : Test if an input is an Armstrong number example
+11.3k Use systeminfo to find out installed Windows Hotfix(s) or updates
+11.4k Android Studio : Create custom icons for your application example
+28.5k Get file path of temporary file in Go
+5.7k Linux : Disable and enable IPv4 forwarding
+10.8k Golang : Removes punctuation or defined delimiter from the user's input
+12.3k Golang : Extract part of string with regular expression
+4.6k Chrome : How to block socketloop.com links in Google SERP?
+3.6k Java : Get FX sentiment from website example
+21.1k Golang : Convert(cast) string to rune and back to string example