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
+23.8k Golang : Convert long hexadecimal with strconv.ParseUint example
+23k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+6.9k Golang Hello World Example
+6.4k Golang : Create zip/ePub file without compression(use Store algorithm)
+11.8k Golang : Convert IPv4 address to packed 32-bit binary format
+9.9k Golang : How to determine a prime number?
+4.8k Fix fatal error: evacuation not done in time problem
+20.5k Golang : Convert seconds to minutes and remainder seconds
+4.1k Golang : Check if a word is countable or not
+5.6k Golang : Find the longest line of text example
+13.8k Golang : Get HTTP protocol version example
+15.5k Golang : Gzip file example