Skip to content

Instantly share code, notes, and snippets.

@julz
Created June 6, 2020 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julz/1b3344aeb9d5142a123b5a427916b70a to your computer and use it in GitHub Desktop.
Save julz/1b3344aeb9d5142a123b5a427916b70a to your computer and use it in GitHub Desktop.
Pick two distinct random numbers efficiently
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