Seven-sided dice from five-sided dice: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Combine into a single block for ease of testing, remove apologies for slow speed as it is much faster now) |
||
Line 1,307: | Line 1,307: | ||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
{{works with|Rakudo |
{{works with|Rakudo|2018.03}} |
||
Since rakudo is still pretty slow, we've done some interesting bits of optimization. |
|||
We factor out the range object construction so that it doesn't have to be recreated each time, and we sneakily <em>subtract</em> the 1's from the 5's, which takes us back to 0 based without having to subtract 6. |
|||
<lang perl6>my $d5 = 1..5; |
<lang perl6>my $d5 = 1..5; |
||
sub d5() { $d5.roll; } # 1d5 |
sub d5() { $d5.roll; } # 1d5 |
||
Line 1,317: | Line 1,316: | ||
$flat = 5 * d5() - d5() until $flat < 21; |
$flat = 5 * d5() - d5() until $flat < 21; |
||
$flat % 7 + 1; |
$flat % 7 + 1; |
||
} |
|||
}</lang> |
|||
Here's the test. We use a C-style for loop, except it's named <code>loop</code>, because it's currently faster than the other loops--and, er, doesn't segfault the GC on a million iterations... |
|||
# Testing |
|||
my @dist; |
|||
my $n = 1_000_000; |
my $n = 1_000_000; |
||
my $expect = $n / 7; |
my $expect = $n / 7; |
||
Line 1,331: | Line 1,331: | ||
{{out}} |
{{out}} |
||
<pre>Expect 142857.143 |
<pre>Expect 142857.143 |
||
1 |
1 143088 +0.16% |
||
2 |
2 143598 +0.52% |
||
3 |
3 141741 -0.78% |
||
4 |
4 142832 -0.02% |
||
5 |
5 143040 +0.13% |
||
6 |
6 142988 +0.09% |
||
7 |
7 142713 -0.10% |
||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |