Skip to content

Instantly share code, notes, and snippets.

@PatrickVienne
Created September 30, 2023 21:41
Show Gist options
  • Save PatrickVienne/d85a8f474560a6d78392ff502859ca5f to your computer and use it in GitHub Desktop.
Save PatrickVienne/d85a8f474560a6d78392ff502859ca5f to your computer and use it in GitHub Desktop.
package rand7
import (
"math/rand"
"testing"
)
func rand5() int {
return rand.Intn(5) + 1
}
func rand7() int {
num := 0
for i := 0; i < 7; i++ {
num += rand5() * i
}
return int(num % 7)
}
func TestRand7(t *testing.T) {
const N = 1000000
counts := map[int]int{}
for i := 0; i < N; i++ {
counts[rand7()]++
}
for i := 1; i < 7; i++ {
println(i, counts[i])
}
/*
OUTPUT:
1 142200
2 142559
3 142895
4 142930
5 143328
6 143498
*/
t.Error("stop the show! :D")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment