blib/lib/Games/Dice/Loaded.pm

filter: Statementstmt 100% 60 / 60 Branchbran 71.4% 10 / 14 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 95.2% 81 / 85 CC 11 SCAR 18.3
CC
11
Cov
95%
__ANON__
9.0
roll
3.0
BEGIN
1.0
CRAP 6.2
↑ Summary
1 package Games::Dice::Loaded;
2 {
3 2 $Games::Dice::Loaded::VERSION = '0.001_01';
4 }
5 2 ▶use Moose 2.0300;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use List::Util qw/max sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Perl extension to simulate rolling loaded dice
10  
11 # Keith Schwarz's article is lovely and has lots of pretty diagrams and proofs,
12 # but unfortunately it's also very long. Here's the tl;dr:
13  
14 # Draw a bar chart of the probabilities of landing on the various sides, then
15 # throw darts at it (by picking X and Y coordinates uniformly at random). If
16 # you hit a bar with your dart, choose that side. This works OK, but has very
17 # bad worst-case behaviour; fortunately, it's possible to cut up the taller
18 # bars and stack them on top of the shorter bars in such a way that the area
19 # covered is exactly a (1/n) \* n rectangle. Constructing this rectangular
20 # "dartboard" can be done in O(n) time, by maintaining a list of short (less
21 # than average height) bars and a list of long bars; add the next short bar to
22 # the dartboard, then take enough of the next long bar to fill that slice up to
23 # the top. Add the index of the long bar to the relevant entry of the "alias
24 # table", then put the remainder of the long bar back into either the list of
25 # short bars or the list of long bars, depending on how long it now is.
26  
27 # Once we've done this, simulating a dice roll can be done in O(1) time:
28 # Generate the dart's coordinates; which vertical slice
29 # did the dart land in, and is it in the shorter bar on the bottom or the
30 # "alias" that's been stacked above it?.
31  
32 # Heights of the lower halves of the strips
33 2 has 'dartboard' => ( is => 'ro', isa => 'ArrayRef' );
34 # Identities of the upper halves of the strips
35 2 has 'aliases' => ( is => 'ro', isa => 'ArrayRef' );
36 2 has 'num_sides' => ( is => 'ro', isa => 'Num' );
37  
38 # Construct the dartboard and alias table
39 around BUILDARGS => sub {
40 2 ▶ my $orig = shift;
Subroutine __ANON__: called CC=9 SCAR=22.0
41 2 my $class = shift;
42 # scale so average weight is 1
43 2 my @weights = @_;
44 2 my $n = scalar @weights;
45 2 my $scalefactor = $n / sum(@weights);
46 2 my $i = 0;
47 2 @weights = map { [$i++, $scalefactor * $_] } @weights;
48 2 my @small = grep { $_->[1] < 1 } @weights;
49 2 my @large = grep { $_->[1] >= 1 } @weights;
50 2 my @dartboard; my @aliases;
51 2 ▶ while ((@small > 0) && (@large > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@small > 0 and @large > 0) 4 2 6
Condition100%3/3
@small > 0 and @large > 0
!ll&&!rl&&r
114
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
52 4 my ($small_id, $small_p) = @{pop @small};
53 4 my ($large_id, $large_p) = @{pop @large};
54 4 $dartboard[$small_id] = $small_p;
55 4 $aliases[$small_id] = $large_id;
56 4 $large_p = $small_p + $large_p - 1;
57 4 ▶ if ($large_p >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($large_p >= 1) { } 3 1 4
58 3 push @large, [$large_id, $large_p];
59 } else {
60 1 push @small, [$large_id, $large_p];
61 }
62 }
63 2 for my $unused (@small, @large) {
64 5 $dartboard[$unused->[0]] = 1;
65 5 $aliases[$unused->[0]] = $unused->[0];
66 }
67 2 for my $side (0 .. $n - 1) {
68 9 my $d = $dartboard[$side];
69 9 ▶ croak("Undefined dartboard for side $side") unless defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 0 9 9
70 9 ▶ croak("Height $d too large for side $side") unless $d <= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $d <= 1 0 9 9
71 9 ▶ croak("Height $d too small for side $side") unless $d >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $d >= 0 0 9 9
72 }
73 2 return $class->$orig(
74 dartboard => \@dartboard,
75 aliases => \@aliases,
76 num_sides => $n,
77 );
78 2 };
79  
80 # Roll the die
81 sub roll {
82 16002 ▶ my ($self) = @_;
Subroutine roll: called CC=3 SCAR=11.0
Pod documented
83 16002 my $side = int(rand $self->num_sides);
84 16002 my $height = rand 1;
85 16002 my @dartboard = @{$self->dartboard()};
86 16002 ▶ croak("Dartboard undefined for side $side")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dartboard[$side] 0 16002 16002
87 unless defined $dartboard[$side];
88 16002 ▶ if ($height > $dartboard[$side]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height > $dartboard[$side]) { } 3509 12493 16002
89 3509 my @aliases = @{$self->aliases};
90 3509 return $aliases[$side] + 1;
91 } else {
92 12493 return $side + 1;
93 }
94 }
95  
96 2 1;
97  
98 __END__
99  
100 =head1 NAME
101  
102 Games::Dice::Loaded - Simulate rolling loaded dice
103  
104 =head1 SYNOPSIS
105  
106 use Games::Dice::Loaded;
107  
108 my $die = Games::Dice::Loaded->new(1/6, 1/6, 1/2, 1/12, 1/12);
109 my $result = $die->roll();
110  
111 my $fair_d4 = Games::Dice::Loaded->new(1, 1, 1, 1);
112 $result = $fair_d4->roll();
113  
114 =head1 DESCRIPTION
115  
116 C<Games::Dice::Loaded> allows you to simulate rolling arbitrarily-weighted dice
117 with arbitrary numbers of sides - or, more formally, to model any discrete
118 random variable which may take only finitely many values. It does this using
119 Vose's elegant I<alias method>, which is described in Keith Schwarz's article
120 L<Darts, Dice, and Coins: Sampling from a Discrete
121 Distribution|http://www.keithschwarz.com/darts-dice-coins/>.
122  
123 =head1 METHODS
124  
125 =over
126  
127 =item new()
128  
129 Constructor. Takes as arguments the probabilities of rolling each "side". If
130 the weights given do not sum to 1, they are scaled so that they do. This method
131 constructs the alias table, in O(num_sides) time.
132  
133 =item roll()
134  
135 Roll the die. Takes no arguments, returns a number in the range 1 .. num_sides. Takes O(1) time.
136  
137 =item num_sides()
138  
139 The number of sides on the die. Read-only.
140  
141 =back
142  
143 =head1 AUTHOR
144  
145 Miles Gould, E<lt>mgould@cpan.orgE<gt>
146  
147 =head1 CONTRIBUTING
148  
149 Please fork
150 L<the GitHub repository|http://github.com/pozorvlak/Games-Dice-Loaded>.
151  
152 =head1 COPYRIGHT AND LICENSE
153  
154 Copyright (C) 2011 by Miles Gould
155  
156 This library is free software; you can redistribute it and/or modify
157 it under the same terms as Perl itself, either Perl version 5.12.4 or,
158 at your option, any later version of Perl 5 you may have available.
159  
160 =head1 SEE ALSO
161  
162 Perl modules for rolling dice:
163 L<Games::Dice>,
164 L<Games::Dice::Advanced>,
165 L<Bot::BasicBot::Pluggable::Module::Dice>,
166 L<random>.
167  
168 A Perl module for calculating probability distributions for dice rolls:
169 L<Games::Dice::Probability>.
170  
171 Descriptions of the alias method:
172  
173 =over
174  
175 =item L<Darts, Dice, and Coins: Sampling from a Discrete
176 Distribution|http://www.keithschwarz.com/darts-dice-coins/>
177  
178 =item L<Data structure for loaded dice?|http://stackoverflow.com/questions/5027757/data-structure-for-loaded-dice> on StackOverflow
179  
180 =item L<Wikipedia article|http://en.wikipedia.org/wiki/Alias_method>
181  
182 =back
183  
184 =cut
↑ Summary