Verify distribution uniformity/Naive: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: added/changed whitespace and comments, elided an execution note, used a template for the output section.) |
|||
Line 1,384: | Line 1,384: | ||
7 143333 +0.33% |
7 143333 +0.33% |
||
</pre> |
</pre> |
||
=={{header|Phix}}== |
|||
<lang Phix>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 |
|||
counts[call_func(fid,{})] += 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(routine_id("rand7"), 7, n, 0.005) |
|||
printf(1,"%d iterations: %s\n",{n,flats[flat+2]}) |
|||
end for</lang> |
|||
{{out}} |
|||
<pre> |
|||
100 iterations: impossible |
|||
1000 iterations: impossible |
|||
10000 iterations: not flat |
|||
100000 iterations: not flat |
|||
1000000 iterations: flat |
|||
10000000 iterations: flat |
|||
</pre> |
|||
At the specified 0.5%, 1000000 iterations is occasionally not flat, and 10000 is sometimes flat at 3%.<br> |
|||
As shown above, it is not mathematically possible to distribute 1000 over 7 bins with <= 0.5% variance.<br> |
|||
At 100 iterations, the permitted range is ~14.21..14.36, so you could not get even one bin right.<br> |
|||
At 1000 iterations, 142 is too low (and 144 too high), they would all have to be 143, but 7*143=1001.<br> |
|||
The commented-out adjustment to n (as Perl 6) changes the "1000 impossible" result to "1001 not flat", <br> |
|||
except of course for the one-in-however-many-gazillion chance of getting exactly 143 of each. |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |