Skip to content

Instantly share code, notes, and snippets.

@piersy
Last active February 21, 2018 10:45
Show Gist options
  • Save piersy/4e67e5ed5806165c1a2ce5558f2b9257 to your computer and use it in GitHub Desktop.
Save piersy/4e67e5ed5806165c1a2ce5558f2b9257 to your computer and use it in GitHub Desktop.
Test evenness of random distribution (are all values from 0 to n equally likely) of golang rand.Intn() - (Result rand.Intn(n) does provide an even distribution)
package main
import (
"fmt"
"math/rand"
)
const (
ChunkSize = (1 << 16)
NumValues = 100
)
func main() {
counts := make(map[int]int)
for i := 0; i < 100; i++ {
for j := 0; j < 1<<16; j++ {
rnd := rand.Intn(100)
counts[rnd] = counts[rnd] + 1
}
}
for i := 0; i < NumValues; i++ {
fmt.Printf("%d: got %d hits\n", i, counts[i]) // kst2 was used to easily visualise the results
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment