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
+14.4k Golang : How to check for empty array string or string?
+9.3k Golang : Resumable upload to Google Drive(RESTful) example
+8.9k Golang : Create unique title slugs example
+10.7k Android Studio : Create custom icons for your application example
+7.6k Golang : Multiplexer with net/http and map
+17.1k Golang : Read data from config file and assign to variables
+12k Golang : Remove or trim extra comma from CSV
+4.2k Mac OSX : Get disk partitions' size, type and name
+17.8k Golang : Get download file size
+5.4k Unix/Linux : How to open tar.gz file ?
+7.4k Golang : Sort words with first uppercase letter
+13.7k Golang : Get uploaded file name or access uploaded files