Games/Dice/Loaded.pm

filter: Statementstmt 100% 58 / 58 Branchbran 71.4% 10 / 14 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 95.1% 78 / 82 CC 11 SCAR 18.9
CC
11
Cov
95%
__ANON__
9.0
roll
3.0
BEGIN
1.0
CRAP 6.6
↑ Summary Test/Probability.pm »
1 package Games::Dice::Loaded;
2 {
3 4 $Games::Dice::Loaded::VERSION = '0.002';
4 }
5 4 ▶use Moose 2.0300;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use List::Util qw/max sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Perl extension to simulate rolling loaded dice
9  
10 # Keith Schwarz's article is lovely and has lots of pretty diagrams and proofs,
11 # but unfortunately it's also very long. Here's the tl;dr:
12  
13 # Draw a bar chart of the probabilities of landing on the various faces, then
14 # throw darts at it (by picking X and Y coordinates uniformly at random). If
15 # you hit a bar with your dart, choose that face. This works OK, but has very
16 # bad worst-case behaviour; fortunately, it's possible to cut up the taller
17 # bars and stack them on top of the shorter bars in such a way that the area
18 # covered is exactly a (1/n) \* n rectangle. Constructing this rectangular
19 # "dartboard" can be done in O(n) time, by maintaining a list of short (less
20 # than average height) bars and a list of long bars; add the next short bar to
21 # the dartboard, then take enough of the next long bar to fill that slice up to
22 # the top. Add the index of the long bar to the relevant entry of the "alias
23 # table", then put the remainder of the long bar back into either the list of
24 # short bars or the list of long bars, depending on how long it now is.
25  
26 # Once we've done this, simulating a dice roll can be done in O(1) time:
27 # Generate the dart's coordinates; which vertical slice did the dart land in,
28 # and is it in the shorter bar on the bottom or the "alias" that's been stacked
29 # above it?
30  
31 # Heights of the lower halves of the strips
32 4 has 'dartboard' => ( is => 'ro', isa => 'ArrayRef' );
33 # Identities of the upper halves of the strips
34 4 has 'aliases' => ( is => 'ro', isa => 'ArrayRef' );
35 4 has 'num_faces' => ( is => 'ro', isa => 'Num' );
36  
37 # Construct the dartboard and alias table
38 4 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
39 4 my $orig = shift;
40 4 my $class = shift;
41 # scale so average weight is 1
42 4 my @weights = @_;
43 4 my $n = scalar @weights;
44 4 my $scalefactor = $n / sum(@weights);
45 4 my $i = 0;
46 4 @weights = map { [$i++, $scalefactor * $_] } @weights;
47 4 my @small = grep { $_->[1] < 1 } @weights;
48 4 my @large = grep { $_->[1] >= 1 } @weights;
49 4 my @dartboard; my @aliases;
50 4 ▶ while ((@small > 0) && (@large > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@small > 0 and @large > 0) 13 4 17
Condition100%3/3
@small > 0 and @large > 0
!ll&&!rl&&r
2213
MC/DC100%2/2
@small > 0 and @large > 0
@small > 0 @large > 0
Truth table100%3/3
A and B
A= @small > 0B= @large > 0
ABresult
0X0
100
111
51 13 my ($small_id, $small_p) = @{pop @small};
52 13 my ($large_id, $large_p) = @{pop @large};
53 13 $dartboard[$small_id] = $small_p;
54 13 $aliases[$small_id] = $large_id;
55 13 $large_p = $small_p + $large_p - 1;
56 13 ▶ if ($large_p >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($large_p >= 1) { } 10 3 13
57 10 push @large, [$large_id, $large_p];
58 } else {
59 3 push @small, [$large_id, $large_p];
60 }
61 }
62 4 for my $unused (@small, @large) {
63 7 $dartboard[$unused->[0]] = 1;
64 7 $aliases[$unused->[0]] = $unused->[0];
65 }
66 4 for my $face (0 .. $n - 1) {
67 20 my $d = $dartboard[$face];
68 20 ▶ die("Undefined dartboard for face $face") unless defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 0 20 20
69 20 ▶ die("Height $d too large for face $face") unless $d <= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $d <= 1 0 20 20
70 20 ▶ die("Height $d too small for face $face") unless $d >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $d >= 0 0 20 20
71 }
72 4 return $class->$orig(
73 dartboard => \@dartboard,
74 aliases => \@aliases,
75 num_faces => $n,
76 );
77 4 };
78  
79 # Roll the die
80 38004 ▶sub roll {
Subroutine roll: called CC=3 SCAR=11.0
Pod documented
81 38004 my ($self) = @_;
82 38004 my $face = int(rand $self->num_faces);
83 38004 my $height = rand 1;
84 38004 my @dartboard = @{$self->dartboard()};
85 38004 ▶ die("Dartboard undefined for face $face")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dartboard[$face] 0 38004 38004
86 unless defined $dartboard[$face];
87 38004 ▶ if ($height > $dartboard[$face]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height > $dartboard[$face]) { } 10368 27636 38004
88 10368 my @aliases = @{$self->aliases};
89 10368 return $aliases[$face] + 1;
90 } else {
91 27636 return $face + 1;
92 }
93 }
94  
95 4 *sample = \&roll;
96  
97 4 1;
98  
99 __END__
↑ Summary Test/Probability.pm »