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
+14k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+7.5k Golang : Rot13 and Rot5 algorithms example
+55.3k Golang : Unmarshal JSON from http response
+5.3k Golang : Generate Interleaved 2 inch by 5 inch barcode
+9k Golang : Build and compile multiple source files
+6.1k Golang : Debug with Godebug
+7.5k Golang : Get YouTube playlist
+8.2k Golang : HttpRouter multiplexer routing example
+10.1k Golang : Print how to use flag for your application example
+34.6k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+11.5k Golang : Change date format to yyyy-mm-dd
+32.2k Golang : Convert []string to []byte examples