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.3k Golang : What is StructTag and how to get StructTag's value?
+11.6k Golang : Gorilla web tool kit secure cookie example
+19k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+7k Golang : Get Alexa ranking data example
+9.4k Mac OSX : Get a process/daemon status information
+20.7k Golang : Convert date string to variants of time.Time type examples
+13.9k Golang : concatenate(combine) strings
+5.5k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+4.6k JavaScript : Rounding number to decimal formats to display currency
+11.1k CodeIgniter : How to check if a session exist in PHP?
+7.7k Golang : Reverse a string with unicode
+10.1k Golang : Check a web page existence with HEAD request example