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
+7.1k Golang : Oanda bot with Telegram and RSI example
+22.2k Golang : Print out struct values in string format
+8.3k Golang : How to generate Code 39 barcode?
+11.4k Swift : Convert (cast) Int or int32 value to CGFloat
+17.8k Golang : How to make function callback or pass value from function as parameter?
+12.5k Golang : Reverse IP address for reverse DNS lookup example
+16.2k Golang : Parse date string and convert to dd-mm-yyyy format
+7.3k Android Studio : Import third-party library or package into Gradle Scripts
+7.7k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+6.7k Gogland : Where to put source code files in package directory for rookie
+8.3k Golang : How to get ECDSA curve and parameters data?