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
+7.9k Golang : Get curl -I or head data from URL example
+21.1k Golang : Repeat a character by multiple of x factor
+15.2k Golang : Test floating point numbers not-a-number and infinite example
+5.2k Golang : Debug with Godebug
+5.8k Golang : Find the shortest line of text example
+7.7k Golang : Set or add headers for many or different handlers
+6.9k Golang : Scan files for certain pattern and rename part of the files
+22.7k Golang : Upload to S3 with official aws-sdk-go package
+3.9k Python : Find out the variable type and determine the type with simple test
+24.9k Golang : Convert(cast) string to uint8 type and back to string