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
+17.4k Golang : Clone with pointer and modify value
+46.2k Golang : Encode image to base64 example
+7.7k Golang : Trim everything onward after a word
+11.6k Golang : Verify Linux user password again before executing a program example
+14.4k Golang : Convert(cast) int to float example
+32.1k Golang : Math pow(the power of x^y) example
+10k Golang : Check a web page existence with HEAD request example
+19.4k Golang : Example for DSA(Digital Signature Algorithm) package functions
+7.7k Setting $GOPATH environment variable for Unix/Linux and Windows
+4.6k PHP : Extract part of a string starting from the middle
+4.9k Golang : micron to centimeter example
+13.7k Golang : Get dimension(width and height) of image file