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


 
Randomize Timer
Function dice5() As Integer
    Return Int(Rnd * 5) + 1
End Function
 
Function dice7() As Integer
    Dim As Integer temp
    Do
        temp = dice5() * 5 + dice5() -6
    Loop Until temp < 21
    Return (temp Mod 7) +1
End Function
 
Function distCheck(n As Ulongint, delta As Double) As Ulongint
 
    Dim As Ulongint a(n)
    Dim As Ulongint maxBucket = 0
    Dim As Ulongint minBucket = 1000000
    For i As Ulongint = 1 To n
        a(i) = dice5()
        If a(i) > maxBucket Then maxBucket = a(i)
        If a(i) < minBucket Then minBucket = a(i)
    Next i
 
    Dim As Ulongint nBuckets = maxBucket + 1
    Dim As Ulongint buckets(maxBucket)
    For i As Ulongint = 1 To n
        buckets(a(i)) += 1
    Next i
    'check buckets
    Dim As Ulongint expected = n / (maxBucket-minBucket+1)
    Dim As Ulongint minVal = Int(expected*(1-delta))
    Dim As Ulongint maxVal = Int(expected*(1+delta))
    expected = Int(expected)
    Print "minVal", "Expected", "maxVal"
    Print minVal, expected, maxVal
    Print "Bucket", "Counter", "pass/fail"
    distCheck = true
    For i As Ulongint = 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 Return false
    Next i
End Function
 
Dim Shared As Ulongint n = 1000
Print "Testing ";n;" times"
If Not(distCheck(n, 0.05)) Then Print "Test failed" Else Print "Test passed"
Print
 
n = 10000
Print "Testing ";n;" times"
If Not(distCheck(n, 0.05)) Then Print "Test failed" Else Print "Test passed"
Print
 
n = 50000
Print "Testing ";n;" times"
If Not(distCheck(n, 0.05)) Then Print "Test failed" Else Print "Test passed"
Print
Sleep
 