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
+8.3k Golang : Read file with ioutil
+6.4k Golang : Create zip/ePub file without compression(use Store algorithm)
+5k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+7.9k Golang : GMail API create and send draft with simple upload attachment example
+9.3k Golang : Meaning of omitempty in struct's field tag
+4.2k Python : Create Whois client or function example
+32.4k Golang : Create x509 certificate, private and public keys
+3.9k Golang : Experimental Jawi programming language
+6.3k Golang : Of hash table and hash map
+6.8k Golang : Error reading timestamp with GORM or SQL driver
+19.2k Golang : Pipe output from one os.Exec(shell command) to another command