Verify distribution uniformity/Naive: Difference between revisions

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
 
(5 intermediate revisions by 4 users not shown)
Line 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++}}==
Line 392 ⟶ 446:
4 200016
5 200424</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func dice5 .
return randint 5
.
func dice25 .
return (dice5 - 1) * 5 + dice5
.
func dice7a .
return dice25 mod1 7
.
func dice7b .
repeat
h = dice25
until h <= 21
.
return h mod1 7
.
numfmt 3 0
#
proc checkdist dicefunc n delta . .
len dist[] 7
for i to n
# no function pointers
if dicefunc = 1
h = dice7a
else
h = dice7b
.
dist[h] += 1
.
for i to len dist[]
h = dist[i] / n * 7
if abs (h - 1) > delta
bad = 1
.
dist[i] = 0
print h
.
if bad = 1
print "-> not uniform"
else
print "-> uniform"
.
print ""
.
#
checkdist 1 1000000 0.01
checkdist 2 1000000 0.01
</syntaxhighlight>
{{out}}
<pre>
1.122
1.121
1.117
1.120
0.844
0.838
0.837
-> not uniform
 
0.997
1.000
1.004
1.001
0.997
1.001
1.000
-> uniform
</pre>
 
=={{header|Elixir}}==
Line 1,872 ⟶ 1,997:
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>
 
Line 2,059 ⟶ 2,215:
Smooth!
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">import rand
import rand.seed
import math
Line 2,123 ⟶ 2,279:
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
import "./sort" for Sort
 
var r = Random.new()
2,083

edits