Seven-sided dice from five-sided dice: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
(Added Wren) |
||
Line 1,996: | Line 1,996: | ||
5 with probability 1/7 + ( 51 ppm) |
5 with probability 1/7 + ( 51 ppm) |
||
6 with probability 1/7 - ( 109 ppm) |
6 with probability 1/7 - ( 109 ppm) |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{trans|Kotlin}} |
|||
{{libheader|Wren-sort}} |
|||
{{libheader|Wren-fmt}} |
|||
<lang ecmascript>import "random" for Random |
|||
import "/sort" for Sort |
|||
import "/fmt" for Fmt |
|||
var r = Random.new() |
|||
var dice5 = Fn.new { r.int(1, 6) } |
|||
var dice7 = Fn.new { |
|||
while (true) { |
|||
var t = (dice5.call() - 1) * 5 + dice5.call() - 1 |
|||
if (t < 21) return 1 + (t/3).floor |
|||
} |
|||
} |
|||
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 |
|||
var cmp = Fn.new { |me1, me2| (me1.key - me2.key).sign } |
|||
occurs = occurs.toList |
|||
Sort.insertion(occurs, cmp) |
|||
for (me in occurs) { |
|||
var k = me.key |
|||
var v = me.value |
|||
var error = (v - expected).abs |
|||
var acceptable = (error <= maxError) ? "Yes" : "No" |
|||
if (acceptable == "No") allAcceptable = false |
|||
Fmt.print(f, k, v, error, acceptable) |
|||
} |
|||
System.print("\nAcceptable overall: %(allAcceptable ? "Yes" : "No")") |
|||
} |
|||
checkDist.call(dice7, 1400000, 0.5)</lang> |
|||
{{out}} |
|||
<pre> |
|||
Repetitions = 1400000, Expected = 200000 |
|||
Tolerance = 0.5%, Max Error = 1000 |
|||
Integer Occurrences Error Acceptable |
|||
1 199744 256 Yes |
|||
2 199678 322 Yes |
|||
3 200254 254 Yes |
|||
4 199903 97 Yes |
|||
5 200080 80 Yes |
|||
6 200070 70 Yes |
|||
7 200271 271 Yes |
|||
Acceptable overall: Yes |
|||
</pre> |
</pre> |
||