Seven-sided dice from five-sided dice: Difference between revisions

m
→‎{{header|Go}}: library changes
m (→‎{{header|Go}}: library changes)
Line 525:
"fmt"
"math"
"math/rand"
"time"
)
 
Line 554 ⟶ 555:
// parameter "delta."
func distCheck(f func() int, n int,
repeats int, delta float64) (max float64, flatEnough bool) {
count := make([]int, n)
for i := 0; i < repeats; i++ {
Line 561 ⟶ 562:
expected := float64(repeats) / float64(n)
for _, c := range count {
max = math.FmaxMax(max, math.FabsAbs(float64(c)-expected))
}
return max, max < delta
Line 567 ⟶ 568:
 
// Driver, produces output satisfying both tasks.
//
// Go global random number (used by dice5) is always initialized with the
// same seed, to allow repeatability. By happy coincidence, a threshold
// of 500 nicely shows one run where the distribution is "flat enough"
// and one where it isn't.
func main() {
rand.Seed(time.Now().UnixNano())
const calls = 1000000
max, flatEnough := distCheck(dice7, 7, calls, 500)
Line 581 ⟶ 578:
Output:
<pre>
Max delta: 677356.85714285713041428571428696 Flat enough: falsetrue
Max delta: 446787.14285714286968571428571304 Flat enough: truefalse
</pre>
 
1,707

edits