Verify distribution uniformity/Naive: Difference between revisions

Liberty BASIC entry
m (→‎{{header|REXX}}: used a simplier (and faster) form of a DO loop.)
(Liberty BASIC entry)
Line 877:
 
distribution potentially skewed for 0: expected result around 50000, got 95040</pre>
 
=={{header|Liberty BASIC}}==
LB cannot pass user-defined function by name, so we use predefined function name - GENERATOR
<lang lb>
'for i = 1 to 20
' print GENERATOR()
'next
'end
'[RC] Verify distribution uniformity/Naive
'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.
' Since LB cannot pass a function, we 'll use given name GENERATOR
' 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.
 
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
</lang>
{{Out}}
<pre>
Testing 1000 times
minVal Expected maxVal
190 200 210
Bucket Counter pass/fail
1 213 fail
2 204
3 192
4 188 fail
5 203
Test failed
 
Testing 10000 times
minVal Expected maxVal
1900 2000 2100
Bucket Counter pass/fail
1 2041
2 1952
3 1975
4 2026
5 2006
Test passed
 
Testing 50000 times
minVal Expected maxVal
9500 10000 10500
Bucket Counter pass/fail
1 10012
2 10207
3 10009
4 9911
5 9861
Test passed
</pre>
 
=={{header|Mathematica}}==
Anonymous user