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
+21.7k Golang : Gorilla mux routing example
+19.9k Golang : GORM create record or insert new record into database example
+4.5k Swift : Convert string array to array example
+11k Golang : Encrypt and decrypt data with x509 crypto
+2k Golang : Fix go-cron set time not working issue
+12.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+9.6k Golang : Command line file upload program to server example
+5.9k Golang : Decode XML data from RSS feed
+48.7k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+14k Golang : Get digits from integer before and after given position example
+4.6k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+14.9k Golang : Get IP addresses of a domain name