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
+10.4k Golang : Simple File Server
+10.2k Golang : Generate random integer or float number
+5.2k Golang : Get S3 or CloudFront object or file information
+4.9k Golang : Convert lines of string into list for delete and insert operation
+5.2k Golang : Intercept, inject and replay HTTP traffics from web server
+20k nginx: [emerg] unknown directive "passenger_enabled"
+21.9k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+27.8k Golang : Connect to database (MySQL/MariaDB) server
+31.2k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+14.2k Golang : Send email with attachment(RFC2822) using Gmail API example
+4.6k HTTP common errors and their meaning explained
+7.7k Golang : Grayscale Image