This task is an adjunct to Seven-sided dice from five-sided dice.



Task

Create a function to check that the random integers returned from a small-integer generator function have uniform distribution.



The function should take as arguments:

 The function (or object) producing random integers.
 The number of times to call the integer generator.
 A 'delta' value of some sort that indicates how close to a flat distribution is close enough.


The function should produce:

 Some indication of the distribution achieved.
 An 'error' if the distribution is not flat enough.


Show the distribution checker working when the produced distribution is flat enough and when it is not. (Use a generator from Seven-sided dice from five-sided dice).



See also:

Verify distribution uniformity/Chi-squared test


with javascript_semantics
function check(integer fid, range, iterations, atom delta)
--
-- fid: routine_id of function that yields integer 1..range
-- range: the maximum value that is returned from fid
-- iterations: number of iterations to test
-- delta: variance, for example 0.005 means 0.5%
--
-- returns -1/0/1 for impossible/not flat/flat.
--
    atom av = iterations/range  -- average/expected value
 
    if floor(av)<av-delta*av
    or ceil(av)>av+delta*av then
        return -1   -- impossible
    end if
    sequence counts = repeat(0,range)
    for i=1 to iterations do
        integer cdx = fid()
        counts[cdx] += 1
    end for
    atom max_delta = max(sq_abs(sq_sub(counts,av)))
    return max_delta<delta*av
end function
 
function rand7()
  return rand(7)
end function
 
constant flats = {"impossible","not flat","flat"}
for p=2 to 7 do
    integer n = power(10,p)
--  n = n+7-remainder(n,7)
    integer flat = check(rand7, 7, n, 0.005)
    printf(1,"%d iterations: %s\n",{n,flats[flat+2]})
end for
