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

→‎{{header|Perl 6}}: Combine into a single block for ease of testing, remove apologies for slow speed as it is much faster now
No edit summary
(→‎{{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:
 
=={{header|Perl 6}}==
{{works with|Rakudo Star|20102018.0903}}
 
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;
sub d5() { $d5.roll; } # 1d5
Line 1,317 ⟶ 1,316:
$flat = 5 * d5() - d5() until $flat < 21;
$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
<lang perl6>my @dist;
my $n = 1_000_000;
my $expect = $n / 7;
Line 1,331:
{{out}}
<pre>Expect 142857.143
1 142835143088 -+0.0216%
2 143021143598 +0.1152%
3 142771141741 -0.0678%
4 142706142832 -0.1102%
5 143258143040 +0.2813%
6 142485142988 -+0.2609%
7 142924142713 +-0.0510%</pre>
</pre>
 
=={{header|PicoLisp}}==
10,327

edits