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
+29.3k Golang : How to convert(cast) string to IP address?
+3.9k Unix/Linux : How to pipe/save output of a command to file?
+10k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+9k CodeIgniter : Load different view for mobile devices
+14.1k Golang : How to login and logout with JWT example
+28k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+7.6k Golang : Combine slices but preserve order example
+12.3k Golang : Verify token from Google Authenticator App
+16.7k Golang : Get command line arguments
+16.8k Golang : Display list of time zones with GMT
+8.3k Golang : Changing a RGBA image number of channels with OpenCV
+9.4k Golang : Select region of interest with mouse click and crop from image