Seven-sided dice from five-sided dice: Difference between revisions

m (syntax highlighting fixup automation)
Line 1,171:
7 142619 </pre>
 
=={{header|Juliajq}}==
{{works with|jq}}
<syntaxhighlight lang="julia">dice5() = rand(1:5)
'''Also works with gojq, the Go implementation of jq.'''
 
In this entry, the results for both a low-entropy and a
function dice7()
high-entropy 5-sided die are shown. The former uses the computer's
r = 5*dice5() + dice5() - 6
clock as a not-very-good PRNG, and the latter uses /dev/random in accordance
r < 21 ? (r%7 + 1) : dice7()
with the following invocation:
end</syntaxhighlight>
<syntaxhighlight lang="julia"sh>dice5() = rand(1:5)
Distribution check:
#!/bin/bash
<pre>julia> hist([dice5() for i=1:10^6])
< /dev/random tr -cd '0-9' | fold -w 1 | jq -Mcnr -f dice.jq
(0:1:5,[199932,200431,199969,199925,199743])
end</syntaxhighlight>
The results employ a two-tailed χ2-test at the 95% confidence level
according to which we are entitled to reject the null hypothesis of
uniform randomness if the χ2 statistic is less than 1.69 or greater
than 16.013.
See https://www.itl.nist.gov/div898/handbook/eda/section3/eda3674.htm
 
'''dice.jq'''
julia> hist([dice7() for i=1:10^6])
<syntaxhighlight lang=jq>
(0:1:7,[142390,143032,142837,142999,142800,142642,143300])</pre>
# Output: a PRN in range(0;$n) where $n is .
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
# Emit a stream of [value, frequency] pairs
def histogram(stream):
reduce stream as $s ({};
($s|type) as $t
| (if $t == "string" then $s else ($s|tojson) end) as $y
| .[$t][$y][0] = $s
| .[$t][$y][1] += 1 )
| .[][] ;
 
# sum of squares
def ss(s): reduce s as $x (0; . + ($x * $x));
 
def chiSquared($expected):
debug # show the actual frequencies
| ss( .[] - $expected ) / $expected;
 
# The high-entropy 5-sided die
def dice5: 1 + (5|prn);
 
# The low-entropy 5-sided die
def pseudo_dice5:
def r: (now * 100000 | floor) % 10;
null | until(. and (. < 5); r) | 1 + . ;
 
# The formal argument dice5 should behave like a 5-sided dice:
def dice7(dice5):
1 + ([limit(7; repeat(dice5))]|add % 7) ;
 
# Issue a report on the results of a sequence of $n trials using the specified dice
def report(dice; $n):
1.69 as $lower
| 16.013 as $upper
| [histogram( limit($n; repeat(dice)) ) | last]
| chiSquared($n/7) as $x2
| "The χ2 statistics for a trial of \($n) virtual tosses is \($x2).",
"Using a two-sided χ2-test with seven degrees of freedom (\($lower), \($upper)), it is reasonable to conclude that",
(if $x2 < $lower then "this is lower than would be expected for a fair die."
elif $x2 > $upper then "this is higher than would be expected for a fair die."
else "this is consistent with the die being fair."
end) ;
 
def report($n):
"Low-entropy die results:",
report(dice7(pseudo_dice5); $n),
"",
"High-entropy die results:",
report(dice7(dice5); $n) ;
 
report(70)
</syntaxhighlight>
{{output}}
<pre>
Low-entropy die results:
["DEBUG:",[19,14,6,18,7,5,1]]
The χ2 statistics for a trial of 70 virtual tosses is 29.2.
Using a two-sided χ2-test with six degrees of freedom (1.69, 16.013), it is reasonable to conclude that
this is higher than would be expected for a fair die.
 
High0entropy die results:
["DEBUG:",[9,11,9,10,15,11,5]]
The χ2 statistics for a trial of 70 virtual tosses is 5.4.
Using a two-sided χ2-test with six degrees of freedom (1.69, 16.013), it is reasonable to conclude that
this is consistent with the die being fair.
</pre>
 
=={{header|Kotlin}}==
2,442

edits