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
+10.5k Golang : Check if an integer is negative or positive
+3.1k Golang : Markov chains to predict probability of next state example
+6.4k Golang : Select region of interest with mouse click and crop from image
+9.5k Golang : How to check if a string starts or ends with certain characters or words?
+3.7k Golang : Example of how to detect which type of script a word belongs to
+16.9k Golang : Randomly pick an item from a slice/array example
+8k Golang : Removes punctuation or defined delimiter from the user's input
+2.9k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+3.2k Unix/Linux : secure copying between servers with SCP command examples
+13.1k Golang : Find IP address from string
+12.8k Golang : How to generate QR codes?
+2.9k Golang : Pad file extension automagically