Verify distribution uniformity/Naive: Difference between revisions

m
(→‎{{header|REXX}}: changed comments and whitespace, changed some variable names to be more descriptive, added commas to output for easier reading the results.)
m (→‎{{header|Wren}}: Minor tidy)
 
(33 intermediate revisions by 17 users not shown)
Line 4:
 
 
;TtaskTask:
Create a function to check that the random integers returned from a small-integer generator function have uniform distribution.
 
Line 12:
* 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]]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F dice5()
R random:(1..5)
 
F distcheck(func, repeats, delta)
V bin = DefaultDict[Int, Int]()
L 1..repeats
bin[func()]++
V target = repeats I/ bin.len
V deltacount = Int(delta / 100.0 * target)
assert(all(bin.values().map(count -> abs(@target - count) < @deltacount)), ‘Bin distribution skewed from #. +/- #.: #.’.format(target, deltacount, sorted(bin.items()).map((key, count) -> (key, @target - count))))
print(bin)
 
distcheck(dice5, 1000000, 1)</syntaxhighlight>
 
{{out}}
<pre>
DefaultDict([1 = 199586, 2 = 200094, 3 = 198933, 4 = 200824, 5 = 200563])
</pre>
 
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Discrete_Random, Ada.Text_IO;
 
procedure Naive_Random is
Line 75 ⟶ 100:
 
Ada.Text_IO.Put_Line("Test Passed? (" & Boolean'Image(OK) & ")");
end Naive_Random;</langsyntaxhighlight>
 
Sample run 1 (all buckets good):<pre>7
Line 106 ⟶ 131:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox, % DistCheck("dice7",10000,3)
 
DistCheck(function, repetitions, delta)
Line 128 ⟶ 153:
}
Return, text
}</langsyntaxhighlight>
<pre>Distribution check:
 
Line 145 ⟶ 170:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> MAXRND = 7
FOR r% = 2 TO 5
check% = FNdistcheck(FNdice5, 10^r%, 0.05)
Line 171 ⟶ 196:
= s%
DEF FNdice5 = RND(5)</langsyntaxhighlight>
Output:
<pre>
Line 181 ⟶ 206:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <math.h>
Line 226 ⟶ 251:
 
return 0;
}</langsyntaxhighlight>output<pre>
Count = 10: bin 1 out of range: 1 (-30% vs 3%), NOT flat
Count = 100: bin 1 out of range: 11 (-23% vs 3%), NOT flat
Line 234 ⟶ 259:
Count = 1000000: flat
</pre>
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Linq;
 
public class Test
{
static void DistCheck(Func<int> func, int nRepeats, double delta)
{
var counts = new Dictionary<int, int>();
 
for (int i = 0; i < nRepeats; i++)
{
int result = func();
if (counts.ContainsKey(result))
counts[result]++;
else
counts[result] = 1;
}
 
double target = nRepeats / (double)counts.Count;
int deltaCount = (int)(delta / 100.0 * target);
 
foreach (var kvp in counts)
{
if (Math.Abs(target - kvp.Value) >= deltaCount)
Console.WriteLine("distribution potentially skewed for '{0}': '{1}'", kvp.Key, kvp.Value);
}
 
foreach (var key in counts.Keys.OrderBy(k => k))
{
Console.WriteLine("{0} {1}", key, counts[key]);
}
}
 
public static void Main(string[] args)
{
DistCheck(() => new Random().Next(1, 6), 1_000_000, 1);
}
}
</syntaxhighlight>
{{out}}
<pre>
1 200274
2 199430
3 199418
4 200473
5 200405
 
</pre>
 
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <map>
#include <iostream>
#include <cmath>
Line 266 ⟶ 345:
 
return good;
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
The code could be shortened if the verify function did the output itself, but the "Clojure way" is to have functions, whenever possible, avoid side effects (like printing) and just return a result. Then the "application-level" code uses doseq and println to display the output to the user. The built-in (rand-int) function is used for all three runs of the verify function, but first with small N to simulate experimental error in a small sample size, then with larger N to show it working properly on large N.
<langsyntaxhighlight lang="clojure">(defn verify [rand n & [delta]]
(let [rands (frequencies (repeatedly n rand))
avg (/ (reduce + (map val rands)) (count rands))
Line 281 ⟶ 360:
[num count okay?] (verify #(rand-int 7) n)]
(println "Saw" num count "times:"
(if okay? "that's" " not") "acceptable"))</langsyntaxhighlight>
 
<pre>Saw 1 13 times: that's acceptable
Line 307 ⟶ 386:
=={{header|Common Lisp}}==
{{trans|OCaml}}
<langsyntaxhighlight lang="lisp">(defun check-distribution (function n &optional (delta 1.0))
(let ((bins (make-hash-table)))
(loop repeat n do (incf (gethash (funcall function) bins 0)))
Line 315 ⟶ 394:
do (format t "~&Distribution potentially skewed for ~w:~
expected around ~w got ~w." key target value)
finally (return bins))))</langsyntaxhighlight>
 
<pre>> (check-distribution 'd7 1000)
Line 330 ⟶ 409:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.math, std.algorithm, std.traits;
 
/**
Line 360 ⟶ 439:
distCheck(() => uniform(1, 6), 1_000_000, 1);
}
}</langsyntaxhighlight>
If compiled with -version=verify_distribution_uniformity_naive_main:
{{out}}
Line 370 ⟶ 449:
=={{header|Elixir}}==
{{trans|Erlang}}
<langsyntaxhighlight lang="elixir">defmodule VerifyDistribution do
def naive( generator, times, delta_percent ) do
dict = Enum.reduce( List.duplicate(generator, times), Map.new, &update_counter/2 )
Line 391 ⟶ 470:
fun = fn -> Dice.dice7 end
IO.inspect VerifyDistribution.naive( fun, 100000, 3 )
IO.inspect VerifyDistribution.naive( fun, 100, 3 )</langsyntaxhighlight>
 
{{out}}
Line 402 ⟶ 481:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( verify_distribution_uniformity ).
 
Line 423 ⟶ 502:
 
update_counter( Fun, Dict ) -> dict:update_counter( Fun(), 1, Dict ).
</syntaxhighlight>
</lang>
 
{{out}}
Line 441 ⟶ 520:
Following the task verbatim.
 
<syntaxhighlight lang="text">
>function checkrandom (frand$, n:index, delta:positive real) ...
$ v=zeros(1,n);
Line 457 ⟶ 536:
>checkrandom("wrongdice",1000000,1)
0
</syntaxhighlight>
</lang>
 
Checking the dice7 from dice5 generator.
 
<syntaxhighlight lang="text">
>function dice5 () := intrandom(1,1,5);
>function dice7 () ...
Line 471 ⟶ 550:
>checkrandom("dice7",1000000,1)
1
</syntaxhighlight>
</lang>
 
Faster implementation with the matrix language.
 
<syntaxhighlight lang="text">
>function dice5(n) := intrandom(1,n,5)-1;
>function dice7(n) ...
Line 494 ⟶ 573:
>checkrandom(wrongdice(1000000))
0
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: kernel random sequences assocs locals sorting prettyprint
math math.functions math.statistics math.vectors math.ranges ;
IN: rosetta-code.dice7
Line 553 ⟶ 632:
{ 1 10 100 1000 10000 100000 1000000 }
[| times | 0.02 7 [ dice7 ] times verify ] each
;</langsyntaxhighlight>
 
Output:
Line 574 ⟶ 653:
=={{header|Forth}}==
requires Forth200x locals
<langsyntaxhighlight lang="forth">: .bounds ( u1 u2 -- ) ." lower bound = " . ." upper bound = " 1- . cr ;
: init-bins ( n -- addr )
cells dup allocate throw tuck swap erase ;
Line 597 ⟶ 676:
and
loop
bins free throw ;</langsyntaxhighlight>
{{output}}
<pre>cr ' d7 1000000 7 1 check-distribution .
Line 620 ⟶ 699:
7 : 1416 ok
0</pre>
 
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">subroutine distcheck(randgen, n, delta)
 
interface
Line 667 ⟶ 747:
deallocate(buckets)
end subroutine</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
{{trans|Liberty BASIC}}
<syntaxhighlight lang="freebasic">
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
</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de Liberty BASIC.
</pre>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 725 ⟶ 875:
max, flatEnough = distCheck(dice7, 7, calls, 500)
fmt.Println("Max delta:", max, "Flat enough:", flatEnough)
}</langsyntaxhighlight>
Output:
<pre>
Line 733 ⟶ 883:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import System.Random
import Data.List
import Control.Monad
Line 745 ⟶ 895:
ul = round $ (100 + fromIntegral d)/100 * avg
ll = round $ (100 - fromIntegral d)/100 * avg
return $ map (head &&& (id &&& liftM2 (&&) (>ll)(<ul)).length) group</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="haskell">*Main> mapM_ print .sort =<< distribCheck (randomRIO(1,6)) 100000 3
(1,(16911,True))
(2,(16599,True))
Line 753 ⟶ 903:
(4,(16624,True))
(5,(16526,True))
(6,(16670,True))</langsyntaxhighlight>
 
=={{header|Hy}}==
 
<langsyntaxhighlight lang="lisp">(import [collections [Counter]])
(import [random [randint]])
 
Line 768 ⟶ 918:
(all (list-comp
(<= (- target delta) (/ n repeats) (+ target delta))
[n (.values bins)])))</langsyntaxhighlight>
 
Examples of use:
 
<langsyntaxhighlight lang="lisp">(for [f [
(fn [] (randint 1 10))
(fn [] (if (randint 0 1) (randint 1 9) (randint 1 10)))]]
(print (uniform? f 5000 .02)))</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
This example assumes the random number generator is passed to <code>verify_uniform</code> as a co-expression. The co-expression <code>rnd</code> is prompted for its next value using <code>@rnd</code>. The co-expression is created using <code>create (|?10)</code> where the vertical bar means generate an infinite sequence of what is to its right (in this case, <code>?10</code> generates a random integer in the range [1,10]).
<langsyntaxhighlight Iconlang="icon"># rnd : a co-expression, which will generate the random numbers
# n : the number of numbers to test
# delta: tolerance in non-uniformity
Line 809 ⟶ 959:
then write ("uniform")
else write ("skewed")
end</langsyntaxhighlight>
Output:
<pre>
Line 840 ⟶ 990:
 
The ''delta'' is given as an optional left argument (<code>x</code>), defaulting to 5%. The right argument (<code>y</code>) is any valid argument to the distribution generating verb.
<langsyntaxhighlight lang="j">checkUniform=: adverb define
0.05 u checkUniform y
:
Line 851 ⟶ 1,001:
errmsg assert (delta * expected) > | expected - {:"1 freqtable
freqtable
)</langsyntaxhighlight>
It is possible to use tacit expressions within an explicit definition enabling a more functional and concise style:
<langsyntaxhighlight lang="j">checkUniformT=: adverb define
0.05 u checkUniformT y
:
Line 860 ⟶ 1,010:
errmsg assert ((n % #) (x&*@[ > |@:-) {:"1) freqtable
freqtable
)</langsyntaxhighlight>
Show usage using <code>rollD7t</code> given in [[Seven-dice from Five-dice#J|Seven-dice from Five-dice]]:
<langsyntaxhighlight lang="j"> 0.05 rollD7t checkUniform 1e5
1 14082
2 14337
Line 872 ⟶ 1,022:
0.05 rollD7t checkUniform 1e2
|Distribution is potentially skewed: assert
| errmsg assert(delta*expected)>|expected-{:"1 freqtable</langsyntaxhighlight>
 
 
=={{header|Java}}==
{{trans|D}}
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import static java.lang.Math.abs;
import java.util.*;
import java.util.function.IntSupplier;
Line 906 ⟶ 1,055:
distCheck(() -> (int) (Math.random() * 5) + 1, 1_000_000, 1);
}
}</langsyntaxhighlight>
<pre>1 200439
2 201016
Line 915 ⟶ 1,064:
=={{header|JavaScript}}==
{{trans|Tcl}}
<langsyntaxhighlight lang="javascript">function distcheck(random_func, times, opts) {
if (opts === undefined) opts = {}
opts['delta'] = opts['delta'] || 2;
Line 954 ⟶ 1,103:
} catch (e) {
print(e);
}</langsyntaxhighlight>
Output:
<pre>0 9945
Line 968 ⟶ 1,117:
 
distribution potentially skewed for 0: expected result around 50000, got 95040</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Printf
 
function distcheck(f::Function, rep::Int=10000, Δ::Int=3)
smpl = f(rep)
counts = Dict(k => count(smpl .== k) for k in unique(smpl))
expected = rep / length(counts)
lbound = expected * (1 - 0.01Δ)
ubound = expected * (1 + 0.01Δ)
noobs = count(x -> !(lbound ≤ x ≤ ubound), values(counts))
if noobs > 0 warn(@sprintf "%2.4f%% values out of bounds" noobs / rep) end
return counts
end
 
# Dice5 check
distcheck(x -> rand(1:5, x))
# Dice7 check
distcheck(dice7)</syntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.3
 
import java.util.Random
Line 1,007 ⟶ 1,175:
println()
checkDist(::dice5, 100_000)
}</langsyntaxhighlight>
 
Sample output:
Line 1,038 ⟶ 1,206:
=={{header|Liberty BASIC}}==
LB cannot pass user-defined function by name, so we use predefined function name - GENERATOR
<syntaxhighlight lang="lb">
<lang lb>
n=1000
print "Testing ";n;" times"
Line 1,097 ⟶ 1,265:
GENERATOR = 1+int(rnd(0)*5) '1..5: dice5
end function
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,134 ⟶ 1,302:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">SetAttributes[CheckDistribution, HoldFirst]
CheckDistribution[function_,number_,delta_] :=(Print["Expected: ", N[number/7], ", Generated :",
Transpose[Tally[Table[function, {number}]]][[2]] // Sort]; If[(Max[#]-Min[#])&
[Transpose[Tally[Table[function, {number}]]][[2]]] < delta*number/700, "Flat", "Skewed"])</langsyntaxhighlight>
 
Example usage:
Line 1,150 ⟶ 1,318:
->Expected: 14285.7, Generated :{14182,14186,14240,14242,14319,14407,14424}
->"Flat"</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import tables
 
 
proc checkDist(f: proc(): int; repeat: Positive; tolerance: float) =
 
var counts: CountTable[int]
for _ in 1..repeat:
counts.inc f()
 
let expected = (repeat / counts.len).toInt # Rounded to nearest.
let allowedDelta = (expected.toFloat * tolerance / 100).toInt
var maxDelta = 0
for val, count in counts.pairs:
let d = abs(count - expected)
if d > maxDelta: maxDelta = d
let status = if maxDelta <= allowedDelta: "passed" else: "failed"
echo "Checking ", repeat, " values with a tolerance of ", tolerance, "%."
echo "Random generator ", status, " the uniformity test."
echo "Max delta encountered = ", maxDelta, " Allowed delta = ", allowedDelta
 
 
when isMainModule:
import random
randomize()
proc rand5(): int = rand(1..5)
checkDist(rand5, 1_000_000, 0.5)
</syntaxhighlight>
 
{{out}}
<pre>Checking 1000000 values with a tolerance of 0.5%.
Random generator passed the uniformity test.
Max delta encountered = 659 Allowed delta = 1000</pre>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let distcheck fn n ?(delta=1.0) () =
let h = Hashtbl.create 5 in
for i = 1 to n do
Line 1,170 ⟶ 1,373:
key target value)
) h;
;;</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
This tests the purportedly random 7-sided die with a slightly biased 1000-sided die.
<langsyntaxhighlight lang="parigp">dice5()=random(5)+1;
dice7()={
Line 1,203 ⟶ 1,406:
 
test(dice7, 10^5)
test(()->if(random(1000),random(1000),1), 10^5)</langsyntaxhighlight>
Output:
<pre>Flat with significance 0.2931867820813680387842134664085280183
Line 1,209 ⟶ 1,412:
### user error: Not flat enough, significance only 5.391077606003910233 E-3500006</pre>
 
=={{header|Perl 6}}==
Testing two 'types' of 7-sided dice. Both appear to be fair.
Since the tested function is rolls of a 7 sided die, the test numbers are magnitudes of 10<sup>x</sup> bumped up to the closest multiple of 7. This reduces spurious error from there not being an integer expected value.
{{trans|Raku}}
<lang perl6>my $d7 = 1..7;
<syntaxhighlight lang="perl">sub roll7 { $d7.roll1+int rand(7) };
sub roll5 { 1+int rand(5) }
sub roll7_5 {
while(1) {
my $d7 = (5*&roll5 + &roll5 - 6) % 8;
return $d7 if $d7;
}
}
 
my $threshold = 35;
 
print dist( $_, $threshold, \&roll7 ) for <1001 1000006>;
for 14, 105, 1001, 10003, 100002, 1000006 -> $n
{print dist( $n_, $threshold, \&roll7roll7_5 ) }for <1001 1000006>;
 
sub dist {
 
sub dist my( $n is copy, $threshold, &$producer) )= {@_;
my @dist;
my $result;
my $expect = $n / 7;
say$result .= sprintf "Expect%10d expected\tn", $expect.fmt("%.3f");
 
loopfor ($_ = $n; $n; --1..$n) { @dist[&$producer()]++; }
 
for @dist.kv ->my $i, $v is copy(1..7) {
nextmy unless$v = @dist[$i];
$v //= 0;
my $pct = ($v - $expect)/$expect*100;
printf$result .= sprintf "%d\t %d\t8d %+6.2f1f%% %s\n", $i, $v, $pct, (abs($pct) > $threshold ? ' - skewed' : '');
($pct.abs > $threshold ?? '- skewed' !! '');
}
sayreturn ''$result . "\n";
}</langsyntaxhighlight>
{{out}}
Sample output:
<pre> 143 expected
1 144 0.7%
Expect 2.000
12 137 2 +0-4.002%
23 3 121 +50-15.004% - skewed
34 163 2 14.0% - +0.00%skewed
45 150 2 +04.009%
56 138 3 +50-3.005% - skewed
67 148 0 -1003.005% - skewed
7 2 +0.00%
 
142858 expected
Expect 15.000
1 142332 15 +-0.004%
2 142648 17 +13-0.331% - skewed
3 143615 13 -130.335% - skewed
4 142305 16 +6-0.674% - skewed
5 14 142703 -60.671% - skewed
6 142821 16 +6-0.670% - skewed
7 143582 14 -60.675% - skewed
 
Expect 143.000 expected
1 149 134 -64.292% - skewed
2 159 142 11.2% - -0.70%skewed
3 154 141 -17.407% - skewed
4 137 130 -49.201% - skewed
5 143 142 -0.700%
6 138 170 +18-3.885% - skewed
7 135 128 -10.5.59% - skewed
 
142858 expected
Expect 1429.000
1 1396 142574 -0.2.31%
2 143043 1468 +20.731%
3 1405 142446 -10.683%
4 143325 1442 +0.913%
5 142949 1453 +0.1.68%
6 142990 1417 -0.841%
7 1422 142679 -0.491%</pre>
 
=={{header|Phix}}==
Expect 14286.000
<!--<syntaxhighlight lang="phix">(phixonline)-->
1 14222 -0.45%
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
2 14320 +0.24%
<span style="color: #008080;">function</span> <span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">fid</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">range</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">iterations</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">delta</span><span style="color: #0000FF;">)</span>
3 14326 +0.28%
<span style="color: #000080;font-style:italic;">--
4 14425 +0.97%
-- fid: routine_id of function that yields integer 1..range
5 14140 -1.02%
-- range: the maximum value that is returned from fid
6 14275 -0.08%
-- iterations: number of iterations to test
7 14294 +0.06%
-- delta: variance, for example 0.005 means 0.5%
 
--
Expect 142858.000
-- returns -1/0/1 for impossible/not flat/flat.
1 142510 -0.24%
--</span>
2 142436 -0.30%
<span style="color: #004080;">atom</span> <span style="color: #000000;">av</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">iterations</span><span style="color: #0000FF;">/</span><span style="color: #000000;">range</span> <span style="color: #000080;font-style:italic;">-- average/expected value</span>
3 142438 -0.29%
4 143152 +0.21%
<span style="color: #008080;">if</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">av</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">av</span><span style="color: #0000FF;">-</span><span style="color: #000000;">delta</span><span style="color: #0000FF;">*</span><span style="color: #000000;">av</span>
5 142905 +0.03%
<span style="color: #008080;">or</span> <span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">av</span><span style="color: #0000FF;">)></span><span style="color: #000000;">av</span><span style="color: #0000FF;">+</span><span style="color: #000000;">delta</span><span style="color: #0000FF;">*</span><span style="color: #000000;">av</span> <span style="color: #008080;">then</span>
6 143232 +0.26%
<span style="color: #008080;">return</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- impossible</span>
7 143333 +0.33%
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">range</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">iterations</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">cdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fid</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">cdx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">max_delta</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">,</span><span style="color: #000000;">av</span><span style="color: #0000FF;">)))</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">max_delta</span><span style="color: #0000FF;"><</span><span style="color: #000000;">delta</span><span style="color: #0000FF;">*</span><span style="color: #000000;">av</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">rand7</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">flats</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"impossible"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"not flat"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"flat"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">7</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- n = n+7-remainder(n,7)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">flat</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rand7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.005</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d iterations: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">flats</span><span style="color: #0000FF;">[</span><span style="color: #000000;">flat</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{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 Raku) 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}}==
Line 1,297 ⟶ 1,541:
(one-tenth of a percent), and a 'prg' code body (i.e. an arbitrary number of
executable expressions).
<langsyntaxhighlight PicoLisplang="picolisp">(de checkDistribution (Cnt Pm . Prg)
(let Res NIL
(do Cnt (accu 'Res (run Prg 1) 1))
Line 1,305 ⟶ 1,549:
Max (*/ N (+ 1000 Pm) 1000) )
(for R Res
(prinl (cdr R) " " (if (>= Max (cdr R) Min) "Good" "Bad")) ) ) ) )</langsyntaxhighlight>
Output:
<pre>: (checkDistribution 100000 5 (rand 1 7))
Line 1,317 ⟶ 1,561:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Prototype RandNum_prt()
 
Procedure.s distcheck(*function.RandNum_prt, repetitions, delta.d)
Line 1,347 ⟶ 1,591:
EndProcedure
 
MessageRequester("Results", distcheck(@dice7(), 1000000, 0.20))</langsyntaxhighlight>
A small delta was chosen to increase the chance of a skewed result in the sample output:
<pre>Distribution check:
Line 1,360 ⟶ 1,604:
=={{header|Python}}==
{{works with|Python|3.1}}
<langsyntaxhighlight lang="python">from collections import Counter
from pprint import pprint as pp
 
Line 1,375 ⟶ 1,619:
for key, count in sorted(bin.items()) ]
)
pp(dict(bin))</langsyntaxhighlight>
Sample output:
<pre>>>> distcheck(dice5, 1000000, 1)
Line 1,386 ⟶ 1,630:
for key, count in sorted(bin.items()) ]
AssertionError: Bin distribution skewed from 200 +/- 2: [(1, 4), (2, -33), (3, 6), (4, 11), (5, 12)]</pre>
 
=={{header|Quackery}}==
 
The word <code>distribution</code> tests a specified word (Quackery function) which should return numbers in the range 1 to 7 inclusive. The word <code>dice7</code>, which satisfies this requirement, is defined at [[Seven-sided dice from five-sided dice#Quackery]].
 
<syntaxhighlight lang="quackery"> [ stack [ 0 0 0 0 0 0 0 ] ] is bins ( --> s )
 
[ 7 times
[ 0 bins take
i poke
bins put ] ] is emptybins ( --> )
 
[ bins share over peek
1+ bins take rot poke
bins put ] is bincrement ( n --> )
[ emptybins
over 7 / temp put
swap times
[ over do 1 -
bincrement ]
bins share dup echo cr
witheach
[ temp share - abs
over > if
[ say "Number of "
i^ 1+ echo
say "s is sketchy."
cr ] ]
2drop temp release ] is distribution ( x n n --> )</syntaxhighlight>
 
{{out}}
 
Testing in the Quackery shell.
 
<pre>/O> ' dice7 1000 20 distribution
...
[ 131 123 160 144 156 145 141 ]
 
Stack empty.
 
/O> ' dice7 1000 10 distribution
...
[ 137 138 130 160 143 150 142 ]
Number of 3s is sketchy.
Number of 4s is sketchy.
</pre>
 
=={{header|R}}==
<langsyntaxhighlight lang="r">distcheck <- function(fn, repetitions=1e4, delta=3)
{
if(is.character(fn))
Line 1,407 ⟶ 1,698:
data.frame(value=names(counts), counts=as.vector(counts), status=status)
}
distcheck(dice7.vec)</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 1,414 ⟶ 1,705:
Returns a pair of a boolean stating uniformity and either the "uniform" distribution or a report of the first skew number found.
 
<langsyntaxhighlight lang="racket">#lang racket
(define (pretty-fraction f)
(if (integer? f) f
Line 1,450 ⟶ 1,741:
(test-uniformity/naive straight-die 1000 5)
; Test whether a biased die fails:
(test-uniformity/naive crooked-die 1000 5)</langsyntaxhighlight>
 
{{out}}
Line 1,456 ⟶ 1,747:
'(#t (1 . 169) (2 . 185) (3 . 184) (4 . 163) (5 . 144) (6 . 155))
'(#f . "test-uniformity/naive distribution of #<procedure:crooked-die> potentially skewed for 6. expected 166 2/3 got 262")</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Since the tested function is rolls of a 7 sided die, the test numbers are magnitudes of 10<sup>x</sup> bumped up to the closest multiple of 7. This reduces spurious error from there not being an integer expected value.
<syntaxhighlight lang="raku" line>my $d7 = 1..7;
sub roll7 { $d7.roll };
 
my $threshold = 3;
 
for 14, 105, 1001, 10003, 100002, 1000006 -> $n
{ dist( $n, $threshold, &roll7 ) };
 
 
sub dist ( $n is copy, $threshold, &producer ) {
my @dist;
my $expect = $n / 7;
say "Expect\t",$expect.fmt("%.3f");
loop ($_ = $n; $n; --$n) { @dist[&producer()]++; }
for @dist.kv -> $i, $v is copy {
next unless $i;
$v //= 0;
my $pct = ($v - $expect)/$expect*100;
printf "%d\t%d\t%+.2f%% %s\n", $i, $v, $pct,
($pct.abs > $threshold ?? '- skewed' !! '');
}
say '';
}</syntaxhighlight>
Sample output:
<pre>
Expect 2.000
1 2 +0.00%
2 3 +50.00% - skewed
3 2 +0.00%
4 2 +0.00%
5 3 +50.00% - skewed
6 0 -100.00% - skewed
7 2 +0.00%
 
Expect 15.000
1 15 +0.00%
2 17 +13.33% - skewed
3 13 -13.33% - skewed
4 16 +6.67% - skewed
5 14 -6.67% - skewed
6 16 +6.67% - skewed
7 14 -6.67% - skewed
 
Expect 143.000
1 134 -6.29% - skewed
2 142 -0.70%
3 141 -1.40%
4 137 -4.20% - skewed
5 142 -0.70%
6 170 +18.88% - skewed
7 135 -5.59% - skewed
 
Expect 1429.000
1 1396 -2.31%
2 1468 +2.73%
3 1405 -1.68%
4 1442 +0.91%
5 1453 +1.68%
6 1417 -0.84%
7 1422 -0.49%
 
Expect 14286.000
1 14222 -0.45%
2 14320 +0.24%
3 14326 +0.28%
4 14425 +0.97%
5 14140 -1.02%
6 14275 -0.08%
7 14294 +0.06%
 
Expect 142858.000
1 142510 -0.24%
2 142436 -0.30%
3 142438 -0.29%
4 143152 +0.21%
5 142905 +0.03%
6 143232 +0.26%
7 143333 +0.33%
</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program simulates a number of trials of a random digit and show it's skew %. */
parse arg func times delta seed . /*obtain arguments (options) from C.L. */
if func=='' | func=="," then func= 'RANDOM' /*function not specified? Use default.*/
if times=='' | times=="," then times= 1000000 /*times " " " " */
if delta=='' | delta=="," then delta= 1/2 /*delta% " " " " */
if datatype(seed, 'W') then call random ,,seed /*use some RAND seed for repeatability.*/
highDig=9 9 /*use this var for the highest digit. */
!.=0 0 /*initialize all possible random trials*/
do times /* [↓] perform a bunch of trials. */
if func=='RANDOM' then ?= random(highDig) /*use RANDOM function.*/
else interpret '?=' func "(0,"highDig')' /* " specified " */
!.?= !.? + 1 /*bump the invocation counter.*/
end /*ttimes*/ /* [↑] store trials ───► pigeonholes. */
/* [↓] compute the digit's skewness. */
g= times / (1 + highDig) /*calculate number of each digit throw.*/
w= max(9, length( commas(times) ) ) /*maximum length of number of trials.*/
pad= left('', 9) /*this is used for output indentation. */
say pad 'digit' center(" hits", w) ' skew ' "skew %" 'result' /*header. */
say padsep '─────' center('', w, '─') '──────' "──────" '──────' /*display a separator line. */
/** [↑] show header and the separator.*/
do k=0 to highDig /*process each of the possible digits. */
skew= g - !.k /*calculate the skew for the digit. */
skewPC= (1 - (g - abs(skew)) / g) * 100 /* " " " percentage for dig*/
say pad center(k, 5) right( commas(!.k), w) right(skew, 6) ,
right( format(skewPC, , 3), 6) center( word('ok skewed', 1+(skewPC>delta)), 6)
end /*k*/
say sep /*display a separator line. */
 
y= 5+1+w+1+6+1+6+1+6 /*width + seps*/
say pad '─────' center('', w, '─') '──────' "─────" '──────' /*separator. */
y=5+1+w+1+6+1+6+1+6 /*the width. */
say pad center(" (with " commas(times) ' trials)' , y) /*# trials. */
say pad center(" (skewed when exceeds " delta'%)' , y) /*skewed note.*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: procedure; parse arg _; do njc=length(_'.9';)-3 to 1 by -3; _=insert(',', _, jc); #=123456789end; return _
sep: say pad '─────' center('', w, '─') '──────' "──────" '──────'; return</syntaxhighlight>
e=verify(n, #'0', , verify(n, #"0.",'M') ) - 4
{{out|output|text=&nbsp; when using the default inputs:}}
do j=e to verify(n, #, "M") by -3; _=insert(',', _, j); end; return _</lang>
Execution note: &nbsp; quite a few runs were needed before a skewed result was obtained.
<br><br>
'''output''' when using the default inputs:
<pre>
digit hits skew skew % result
Line 1,516 ⟶ 1,888:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Verify distribution uniformity/Naive
# Date : 2017/09/21
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
 
maxrnd = 7
Line 1,550 ⟶ 1,919:
func dice5
return random(5)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,557 ⟶ 1,926:
Over 10000 runs dice5 passed distribution check
Over 100000 runs dice5 passed distribution check
</pre>
 
=={{header|RPL}}==
Calculated frequencies are negative when below/above the tolerance given by <code>delta</code>.
 
<code>DICE7</code> is defined at [[Seven-sided dice from five-sided dice#RPL|Seven-sided dice from five-sided dice]]
≪ 1 → func n delta bins
≪ { 1 } 0 CON
1 n '''FOR''' j
func EVAL
'''IF''' bins OVER < '''THEN'''
DUP 'bins' STO
1 →LIST RDM bins
'''END'''
DUP2 GET 1 + PUT
'''NEXT'''
1 bins '''FOR''' j
DUP j GET
'''IF'''
DUP n bins / %CH 100 / ABS
delta >
'''THEN''' NEG j SWAP PUT '''ELSE''' DROP '''END'''
'''NEXT'''
≫ ≫ '<span style="color:blue">UNIF?</span>' STO
 
≪ <span style="color:blue">DICE7</span> ≫ 10000 .05 <span style="color:blue">UNIF?</span>
≪ 6 RAND * CEIL ≫ 1000 .05 <span style="color:blue">UNIF?</span>
{{out}}
<pre>
2: [ 1439 1404 1413 1410 1424 1486 1424 ]
1: [ 169 172 -158 163 171 167 ]
</pre>
 
=={{header|Ruby}}==
{{trans|Tcl}}
<langsyntaxhighlight lang="ruby">def distcheck(n, delta=1)
unless block_given?
raise ArgumentError, "pass a block to this method"
Line 1,587 ⟶ 1,987:
p e
end
end</langsyntaxhighlight>
 
{{out}}
Line 1,604 ⟶ 2,004:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">s$ = "#########################"
dim num(100)
for i = 1 to 1000
Line 1,613 ⟶ 2,013:
for i = 1 to 10
print using("###",i);" "; using("#####",num(i));" ";left$(s$,num(i) / 5)
next i</langsyntaxhighlight><pre>
1 90 ##################
2 110 ######################
Line 1,624 ⟶ 2,024:
9 82 ################
10 92 ##################*</pre>
 
=={{header|Scala}}==
===Imperative, ugly, mutable data===
<syntaxhighlight lang="scala">object DistrubCheck1 extends App {
 
private def distCheck(f: () => Int, nRepeats: Int, delta: Double): Unit = {
val counts = scala.collection.mutable.Map[Int, Int]()
 
for (_ <- 0 until nRepeats)
counts.updateWith(f()) {
case Some(count) => Some(count + 1)
case None => Some(1)
}
 
val target: Double = nRepeats.toDouble / counts.size
val deltaCount: Int = (delta / 100.0 * target).toInt
counts.foreach {
case (k, v) =>
if (math.abs(target - v) >= deltaCount)
println(f"distribution potentially skewed for $k%s: $v%d")
}
counts.toIndexedSeq.foreach(entry => println(f"${entry._1}%d ${entry._2}%d"))
}
 
distCheck(() => 1 + util.Random.nextInt(5), 1_000_000, 1)
 
}</syntaxhighlight>
 
===Functional Style===
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/oYJWUvX/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/O513W3VoQ7ulspUMnGvTiQ Scastie (remote JVM)].
<syntaxhighlight lang="scala">object DistrubCheck2 extends App {
private def distCheck(f: () => Int, nRepeats: Int, delta: Double): Unit = {
val counts: Map[Int, Int] =
(0 until nRepeats).map(_ => f()).groupBy(identity).map { case (k, v) => (k, v.size) }
val target = nRepeats / counts.size.toDouble
 
counts.withFilter { case (_, v) => math.abs(target - v) >= (delta / 100.0 * target) }
.foreach { case (k, v) => println(f"distribution potentially skewed for $k%s: $v%d") }
 
counts.toIndexedSeq.foreach(entry => println(f"${entry._1}%d ${entry._2}%d"))
}
 
distCheck(() => 1 + util.Random.nextInt(5), 1_000_000, 1)
 
}</syntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc distcheck {random times {delta 1}} {
for {set i 0} {$i<$times} {incr i} {incr vals([uplevel 1 $random])}
set target [expr {$times / [array size vals]}]
Line 1,636 ⟶ 2,081:
foreach k [lsort -integer [array names vals]] {lappend result $k $vals($k)}
return $result
}</langsyntaxhighlight>
Demonstration:
<langsyntaxhighlight lang="tcl"># First, a uniformly distributed random variable
puts [distcheck {expr {int(10*rand())}} 100000]
 
# Now, one that definitely isn't!
puts [distcheck {expr {rand()>0.95}} 100000]</langsyntaxhighlight>
Which produces this output (error in red):
0 10003 1 9851 2 10058 3 10193 4 10126 5 10002 6 9852 7 9964 8 9957 9 9994
Line 1,648 ⟶ 2,093:
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">Option Explicit
 
sub verifydistribution(calledfunction, samples, delta)
Line 1,671 ⟶ 2,116:
& ", desired limit is " & FormatPercent(delta, 2) & "."
if maxdiff > delta then wscript.echo "Skewed!" else wscript.echo "Smooth!"
end sub</langsyntaxhighlight>
Demonstration with included [[Seven-sided dice from five-sided dice#VBScript]] code:
<langsyntaxhighlight lang="vb">verifydistribution "dice7", 1000, 0.03
verifydistribution "dice7", 100000, 0.03</langsyntaxhighlight>
Which produces this output:
Running "dice7" 1000 times...
Line 1,698 ⟶ 2,143:
Maximum found variation is 0.94%, desired limit is 3.00%.
Smooth!
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">import rand
import rand.seed
import math
// "given"
fn dice5() int {
return rand.intn(5) or {0} + 1
}
// fntion specified by task "Seven-sided dice from five-sided dice"
fn dice7() int {
mut i := 0
for {
i = 5*dice5() + dice5()
if i < 27 {
break
}
}
return (i / 3) - 1
}
// fntion specified by task "Verify distribution uniformity/Naive"
//
// Parameter "f" is expected to return a random integer in the range 1..n.
// (Values out of range will cause an unceremonious crash.)
// "Max" is returned as an "indication of distribution achieved."
// It is the maximum delta observed from the count representing a perfectly
// uniform distribution.
// Also returned is a boolean, true if "max" is less than threshold
// parameter "delta."
fn dist_check(f fn() int, n int,
repeats int, delta f64) (f64, bool) {
mut max := 0.0
mut count := []int{len: n}
for _ in 0..repeats {
count[f()-1]++
}
expected := f64(repeats) / f64(n)
for c in count {
max = math.max(max, math.abs(f64(c)-expected))
}
return max, max < delta
}
// Driver, produces output satisfying both tasks.
fn main() {
rand.seed(seed.time_seed_array(2))
calls := 1000000
mut max, mut flat_enough := dist_check(dice7, 7, calls, 500)
println("Max delta: $max Flat enough: $flat_enough")
max, flat_enough = dist_check(dice7, 7, calls, 500)
println("Max delta: $max Flat enough: $flat_enough")
}</syntaxhighlight>
{{out}}
<pre>
Max delta: 723.8571428571304 Flat enough: false
Max delta: 435.1428571428696 Flat enough: true
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<syntaxhighlight lang="wren">import "random" for Random
import "./fmt" for Fmt
import "./sort" for Sort
 
var r = Random.new()
 
var dice5 = Fn.new { 1 + r.int(5) }
 
var checkDist = Fn.new { |gen, nRepeats, tolerance|
var occurs = {}
for (i in 1..nRepeats) {
var d = gen.call()
occurs[d] = occurs.containsKey(d) ? occurs[d] + 1 : 1
}
var expected = (nRepeats/occurs.count).floor
var maxError = (expected*tolerance/100).floor
System.print("Repetitions = %(nRepeats), Expected = %(expected)")
System.print("Tolerance = %(tolerance)\%, Max Error = %(maxError)\n")
System.print("Integer Occurrences Error Acceptable")
var f = " $d $5d $5d $s"
var allAcceptable = true
occurs = occurs.toList
Sort.quick(occurs)
for (me in occurs) {
var error = (me.value - expected).abs
var acceptable = (error <= maxError) ? "Yes" : "No"
if (acceptable == "No") allAcceptable = false
Fmt.print(f, me.key, me.value, error, acceptable)
}
System.print("\nAcceptable overall: %(allAcceptable ? "Yes" : "No")")
}
 
checkDist.call(dice5, 1e6, 0.5)
System.print()
checkDist.call(dice5, 1e5, 0.5)</syntaxhighlight>
 
{{out}}
Sample run:
<pre>
Repetitions = 1000000, Expected = 200000
Tolerance = 0.5%, Max Error = 1000
 
Integer Occurrences Error Acceptable
1 199599 401 Yes
2 199676 324 Yes
3 200561 561 Yes
4 200647 647 Yes
5 199517 483 Yes
 
Acceptable overall: Yes
 
Repetitions = 100000, Expected = 20000
Tolerance = 0.5%, Max Error = 100
 
Integer Occurrences Error Acceptable
1 19780 220 No
2 20005 5 Yes
3 20206 206 No
4 19920 80 Yes
5 20089 89 Yes
 
Acceptable overall: No
</pre>
 
=={{header|zkl}}==
This tests the random spread over 0..9. It starts at 10 samples and doubles the sample size until the spread is within 0.1% of 10% for each bucket.
<langsyntaxhighlight lang="zkl">fcn rtest(N){
dist:=L(0,0,0,0,0,0,0,0,0,0);
do(N){n:=(0).random(10); dist[n]=dist[n]+1}
Line 1,712 ⟶ 2,285:
 
n:=10;
while(not rtest(n)) {n*=2}</langsyntaxhighlight>
{{out}}
Reported numbers is the percent that bucket has of all samples.
9,476

edits