Seven-sided dice from five-sided dice: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,244: | Line 1,244: | ||
return x%7 + 1 |
return x%7 + 1 |
||
end</lang> |
end</lang> |
||
=={{header|M2000 Interpreter}}== |
|||
<lang M2000 Interpreter> |
|||
We make a stack object (is reference type) and pass it as a closure to dice7 lambda function. For each dice7 we pop the top value of stack, and we add a fresh dice5 (random(1,5)) as last value of stack, so stack used as FIFO. Each time z has the sum of 7 random values. |
|||
We check for uniform numbers using +-5% from expected value. |
|||
Module CheckIt { |
|||
Def long i, calls, max, min |
|||
s=stack:=random(1,5),random(1,5), random(1,5), random(1,5), random(1,5), random(1,5), random(1,5) |
|||
z=0: for i=1 to 7 { z+=stackitem(s, i)} |
|||
dice7=lambda z, s -> { |
|||
=((z-1) mod 7)+1 : stack s {z-=Number : data random(1,5): z+=Stackitem(7)} |
|||
} |
|||
Dim count(1 to 7)=0& ' long type |
|||
calls=700000 |
|||
p=0.05 |
|||
IsUniform=lambda max=calls/7*(1+p), min=calls/7*(1-p) (a)->{ |
|||
if len(a)=0 then =false : exit |
|||
=false |
|||
m=each(a) |
|||
while m |
|||
if array(m)<min or array(m)>max then break |
|||
end while |
|||
=true |
|||
} |
|||
For i=1 to calls {count(dice7())++} |
|||
max=count()#max() |
|||
expected=calls div 7 |
|||
min=count()#min() |
|||
for i=1 to 7 |
|||
document doc$=format$("{0}{1::-7}",i,count(i))+{ |
|||
} |
|||
Next i |
|||
doc$=format$("min={0} expected={1} max={2}", min, expected, max)+{ |
|||
}+format$("Verify Uniform:{0}", if$(IsUniform(count())->"uniform", "skewed"))+{ |
|||
} |
|||
Print |
|||
report doc$ |
|||
clipboard doc$ |
|||
} |
|||
CheckIt |
|||
</lang> |
|||
{{out}} |
|||
<pre style="height:30ex;overflow:scroll"> |
|||
1 9865 |
|||
2 10109 |
|||
3 9868 |
|||
4 9961 |
|||
5 9936 |
|||
6 9922 |
|||
7 10339 |
|||
min=9865 expected=10000 max=10339 |
|||
Verify Uniform:uniform |
|||
1 100081 |
|||
2 99519 |
|||
3 99897 |
|||
4 99823 |
|||
5 100649 |
|||
6 100265 |
|||
7 99766 |
|||
min=99519 expected=100000 max=100649 |
|||
Verify Uniform:uniform |
|||
</pre > |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
<lang Mathematica>sevenFrom5Dice := (tmp$ = 5*RandomInteger[{1, 5}] + RandomInteger[{1, 5}] - 6; |
<lang Mathematica>sevenFrom5Dice := (tmp$ = 5*RandomInteger[{1, 5}] + RandomInteger[{1, 5}] - 6; |