Created
June 6, 2020 08:43
-
-
Save julz/1b3344aeb9d5142a123b5a427916b70a to your computer and use it in GitHub Desktop.
Pick two distinct random numbers efficiently
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
) | |
// (repeatedly) picks 2 distinct random numbers from a range, avoids looping in case of clashes | |
func main() { | |
l := 10 | |
for i := 0; i < 5000; i++ { | |
a, b := rand.Intn(l), rand.Intn(l-1) | |
if b >= a { | |
b++ | |
} | |
fmt.Println(a, b) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment