How do I get a set of random numbers that are not repeated in the set?
Go:
for i := 0; i < 10; i++ {
v := rand.Intn(100)
fmt.Println(v)
}
This gives me, sometimes, two or three of the same numbers. I want all of them different. How do I do this?
For example,
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
p := rand.Perm(100)
for _, r := range p[:10] {
fmt.Println(r)
}
}
Output:
87
75
89
74
17
32
56
44
36
0
Playground:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments