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


 
n=1000
print "Testing ";n;" times"
if  not(check(n, 0.05)) then print "Test failed" else print "Test passed"
print
 
n=10000
print "Testing ";n;" times"
if  not(check(n, 0.05)) then print "Test failed" else print "Test passed"
print
 
n=50000
print "Testing ";n;" times"
if  not(check(n, 0.05)) then print "Test failed" else print "Test passed"
print
 
end
 
function check(n, delta)
    'fill randoms
    dim a(n)
    maxBucket=0
    minBucket=1e10
    for i = 1 to n
        a(i) = GENERATOR()
        if a(i)>maxBucket then maxBucket=a(i)
        if a(i)<minBucket then minBucket=a(i)
    next
    'fill buckets
    nBuckets = maxBucket+1 'from 0
    dim buckets(maxBucket)
    for i = 1 to n
        buckets(a(i)) =  buckets(a(i))+1
    next
    'check buckets
    expected=n/(maxBucket-minBucket+1)
    minVal=int(expected*(1-delta))
    maxVal=int(expected*(1+delta))
    expected=int(expected)
    print "minVal", "Expected", "maxVal"
    print minVal, expected, maxVal
    print "Bucket", "Counter", "pass/fail"
    check = 1
    for i = minBucket to maxBucket
        print i, buckets(i), _
            iif$((minVal > buckets(i)) OR (buckets(i) > maxVal) ,"fail","")
        if (minVal > buckets(i)) OR (buckets(i) > maxVal) then check = 0
    next
end function
 
function iif$(test, valYes$, valNo$)
    iif$ =  valNo$
    if test then iif$ =  valYes$
end function
 
function GENERATOR()
    'GENERATOR = int(rnd(0)*10) '0..9
    GENERATOR = 1+int(rnd(0)*5) '1..5: dice5
end function
 