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
+5.9k Golang : Launching your executable inside a console under Linux
+39.3k Golang : How to read CSV file
+7.1k Golang : Squaring elements in array
+8.2k Golang : Multiplexer with net/http and map
+7.2k Restart Apache or Nginx web server without password prompt
+6.9k Default cipher that OpenSSL used to encrypt a PEM file
+31k Golang : Interpolating or substituting variables in string examples
+4.8k Facebook : How to place save to Facebook button on your website
+7.7k Android Studio : AlertDialog to get user attention example
+5.1k Golang : Constant and variable names in native language