lib/Game/Lottery.pm

filter: Statementstmt 92.8% 130 / 140 Branchbran 87.5% 42 / 48 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 95.2% 20 / 21 Podpod 100% 9 / 9 totaltotal 91.5% 207 / 226 CC 35 SCAR 16.2
CC
35
Cov
91%
SavePicks
20.0
CheckPicks
7.0
TicketValue
5.1
CRAP 5.1
« script/lottery-big-draw.pl ↑ Summary
1 # PODNAME: Game::Lottery
2 # ABSTRACT: Generate and Check Big Draw Lottery Tickets
3  
4 =pod
5  
6 =head1 NAME
7  
8 Game::Lottery
9  
10 =head1 VERSION
11  
12 version 1.04
13  
14 =head1 DESCRIPTION
15  
16 Pick and check numbers for popular Lottery Draw Games using a Cryptographically Secure randomizer.
17  
18 General Draw Games and Big Draw Games are supported, but ticket valuation functions are only available for PowerBall and MegaMillions.
19  
20 =head1 SYNOPSIS
21  
22 Use the bundled lottery-big-draw.pl script to pick your numbers:
23  
24 lottery-big-draw.pl -h
25  
26 To use in your code:
27  
28 use Game::Lottery;
29 my $lottery = Game::Lottery->new( game => 'PowerBall');
30  
31 =head1 DRAW GAMES
32  
33 Lottery Draw games have a pool of balls. Players bet on as many numbers as the number of balls that will be drawn.
34  
35 =head1 BIG DRAW LOTTERY GAMES
36  
37 Have a pool of balls (white) players must match, and a second pool (red) from which (usually) a single ball is drawn, many of the prizes require matching the red ball.
38  
39 The odds of winning the big prize in the two main games in the United States: PowerBall and MegaMillions, are about 300 Million to 1. When the prize isn't won, the jackpot increases. The JackPots frequently get quite large. When the JackPots get large enough, the expected value of a ticket exceeds the cost. The smaller prizes in both US games return less than 20% of the ticket value.
40  
41 There are several options to increase bet sizes, both major games have a multiplier ball option; these are not currently supported.
42  
43 California has a different payout on non-jackpot prizes. For California and any other jurisdictions not using the standard payout table, the expected value generated by this software does not apply.
44  
45 =cut
46  
47 6 ▶use 5.038;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 6 ▶use experimental qw/class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 class Game::Lottery;
51 6 our $VERSION="1.04";
52 6 ▶use Math::Random::Secure;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 6 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 # use Data::Dumper;
55 # use Data::Printer;
56 6 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 6 field $game : param;
59 6 field $wb; # White Balls
60 6 field $rb; # Red Balls
61 6 field $wbd; # White Balls to Draw
62 6 field $rbd; # Red Balls to Draw
63 6 field %coll; # collision prevention
64  
65 14 ▶ADJUST {
Subroutine __ANON__: called CC=5 SCAR=16.1
66 14 ▶ if ( $game =~ /^power/i ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($game =~ /^power/iu) { } 5 9 14
elsif ($game =~ /^mega/iu) { } 4 5 9
elsif ($game =~ /^draw/iu) { } 2 3 5
elsif ($game =~ /^custom/iu) { } 2 1 3
67 5 $game = 'PowerBall';
68 5 $wb = 69;
69 5 $wbd = 5;
70 5 $rb = 26;
71 5 $rbd = 1;
72 }
73 elsif ( $game =~ /^mega/i ) {
74 4 $game = 'MegaMillions';
75 4 $wb = 70;
76 4 $wbd = 5;
77 4 $rb = 25;
78 4 $rbd = 1;
79 }
80 elsif ( $game =~ /^draw/i ) {
81 2 $game = 'Draw';
82 }
83 elsif ( $game =~ /^custom/i ) {
84 2 $game = 'CustomBigDraw';
85 }
86 else {
87 1 die "Unknown GAME $game.\n";
88 }
89 }
90  
91 5041 ▶sub _PickBall ($BallLastNum) {
Subroutine _PickBall: called CC=1 SCAR=0.0
92 # rand starts at 0 -- offset the pick.
93 5041 my $pick = 1 + int Math::Random::Secure::rand($BallLastNum);
94 }
95  
96 12 ▶sub _DrawBalls ( $BallLastNum, $BallsDrawn ) {
Subroutine _DrawBalls: called CC=4 SCAR=13.9
97 12 ▶ unless( $BallLastNum and $BallsDrawn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($BallLastNum and $BallsDrawn) 1 11 12
Condition66%2/3
$BallLastNum and $BallsDrawn
!ll&&!rl&&r
1011
MC/DC50%1/2
$BallLastNum and $BallsDrawn
$BallLastNum $BallsDrawn
Truth table66%2/3
A and B
A= $BallLastNumB= $BallsDrawn
ABresult
0X0
100
111
98 1 die "Balls are not defined. If this is a BigDraw did you use CustomBigDrawSetup?\n";
99 }
100 11 my %balls = ();
101 11 ▶ until ( scalar( keys %balls ) == $BallsDrawn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %balls == $BallsDrawn) 41 11 52
102 41 my $newball = sprintf( "%02d", _PickBall($BallLastNum) );
103 41 $balls{$newball} = $newball;
104 }
105 11 return [ sort { $a <=> $b } ( keys(%balls) ) ];
106 }
107  
108 =head1 METHODS
109  
110 =head2 new
111  
112 Required parameter: game
113  
114 Games currently supported are PowerBall, MegaMillions, Draw (1 ball pool) and CustomBigDraw(custom 2 ball pools). Game names may be abbreviated as mega, power, draw and custom.
115  
116 my $game = Game::Lottery->new( game => 'PowerBall');
117 my $game = Game::Lottery->new( game => 'power');
118  
119 =head2 Game
120  
121 Returns the Game.
122  
123 say $game->Game();
124  
125 =cut
126  
127 11 ▶method _NoCollision ( $balls1, $balls2=[] ) {
Subroutine _NoCollision: called CC=3 SCAR=11.0
128 11 my $key = join '', ($balls1->@*, $balls2->@*);
129 11 ▶ if ( defined $coll{$key }) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $coll{$key}) 2 9 11
130 9 $coll{$key } = 1 ;
131 9 return 1;
132 }
133  
134 2 ▶method Game {
Subroutine Game: called CC=1 SCAR=0.0
Pod documented
135 2 return $game;
136 }
137  
138 =head2 BasicDraw
139  
140 Returns an Array Reference of balls picked. Applicable to a large number of Draw games.
141  
142 my $drawn = $game->BasicDraw( $BallLastNum, $BallsDrawn );
143  
144 =cut
145  
146 2 ▶method BasicDraw ( $BallLastNum, $BallsDrawn ) {
Subroutine BasicDraw: called CC=3 SCAR=11.0
Pod documented
147 2 my $balls = undef;
148 2 ▶ until (defined $balls) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $balls) 2 2 4
149 2 $balls = _DrawBalls( $BallLastNum, $BallsDrawn );
150 2 ▶ $balls = undef unless $self->_NoCollision( $balls );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_NoCollision($balls) 0 2 2
151 }
152 2 $balls;
153 }
154  
155 =head2 BigDraw
156  
157 Draws White and Red Balls for Big Draw Games.
158  
159 Returns a Hash Reference.
160  
161 my $drawn = $game->BigDraw();
162  
163 {
164 game => $game,
165 whiteballs => ArrayRef,
166 redballs => ArrayRef
167 }
168  
169 =head2 CustomBigDrawSetup
170  
171 For CustomBigDraw games, the pool ranges and number of balls to draw must be set before using the BigDraw method. For the directly supported Big Draw Games this is done at new. The game name is set to CustomBigDraw
172  
173 $game->CustomBigDrawSetup(
174 game => 'Some other big draw game', # optionally set a custom game name.
175 white => 99,
176 whitecount => 1,
177 red => 20,
178 redcount => 5
179 );
180  
181 =cut
182  
183 2 ▶method CustomBigDrawSetup (%balls) {
Subroutine CustomBigDrawSetup: called CC=3 SCAR=11.0
Pod documented
184 2 $wb = $balls{'white'};
185 2 $wbd = $balls{'whitecount'};
186 2 $rb = $balls{'red'};
187 2 ▶ $rbd = $balls{'redcount'} || 1;
Condition100%2/2
$balls{'redcount'} || 1
l!l
11
MC/DC100%1/1
$balls{'redcount'} || 1
$balls{'redcount'}
Truth table100%2/2
A
A= $balls{'redcount'}
Aresult
11
00
188 2 ▶ $game = $balls{'game'} if $balls{'game'};
Branch: 1 branch
BranchTrueFalseTotal
if $balls{"game"} 1 1 2
189 }
190  
191 5 ▶method BigDraw {
Subroutine BigDraw: called CC=2 SCAR=7.0
Pod documented
192 5 while (1) {
193 5 my $draw = {
194 'game' => $game,
195 'whiteballs' => _DrawBalls( $wb, $wbd ),
196 'redballs' => _DrawBalls( $rb, $rbd )
197 };
198 4 ▶ if ( $self->_NoCollision( $draw->{whiteballs}, $draw->{redballs} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_NoCollision($draw->{"whiteballs"}, $draw->{"redballs"})) 4 0 4
199 4 return $draw;
200 }
201 }
202 }
203  
204 8 ▶sub _round_val ($val) { sprintf( "%.2f", $val ) }
Subroutine _round_val: called CC=1 SCAR=0.0
205  
206 # Calculate the
207 3 ▶sub _BaseMMVal {
Subroutine _BaseMMVal: called CC=1 SCAR=0.0
208 3 my $val = 2 * ( 1 / 37 ); # Red
209 3 $val += 4 * ( 1 / 89 ); # Red + 1 White
210 3 $val += 10 * ( 1 / 693 ); # Red + 2 White
211 3 $val += 10 * ( 1 / 606 ); # 3 White
212 3 $val += 200 * ( 1 / 14547 ); # Red + 3 White
213 3 $val += 500 * ( 1 / 38792 ); # 4 White
214 3 $val += 10000 * ( 1 / 931001 ); # Red + 4 White
215 3 $val += 10**6 * ( 1 / 12607306 ); # 5 White
216 3 return $val;
217 }
218  
219 3 ▶sub _BasePBVal {
Subroutine _BasePBVal: called CC=1 SCAR=0.0
220 3 my $val = 4 * ( 1 / 38.32 ); # Red
221 3 $val += 4 * ( 1 / 91.98 ); # Red + 1 White
222 3 $val += 7 * ( 1 / 701.33 ); # Red + 2 White
223 3 $val += 7 * ( 1 / 579.76 ); # 3 White
224 3 $val += 100 * ( 1 / 1494.11 ); # Red + 3 White
225 3 $val += 100 * ( 1 / 36525.17 ); # 4 White
226 3 $val += 50000 * ( 1 / 913129.18 ); # Red + 4 White
227 3 $val += 10**6 * ( 1 / 11688053.52 ); # 5 White
228 3 return $val;
229 }
230  
231 =head2 TicketValue
232  
233 Returns the expected value of a ticket given a JackPot. The Cash Value should be used, not the advertised annuity value. The value passed may be either in dollars or millions of dollars. If no value (or 0) is based the result will be the return of all other prizes than the jackpot.
234  
235 my $TicketValue = $game->TicketValue( 600 );
236 my $BaseTicketValue = $game->TicketValue( 0 );
237  
238 =head2 TicketJackPotValue
239  
240 Returns just value of the JackPot divided by the number of possible combinations.
241  
242 my $JackPot = $game->TicketJackPotValue( 600 );
243  
244 =cut
245  
246 4 ▶method TicketValue ( $jackpot = 0 ) {
Subroutine TicketValue: called CC=5 SCAR=16.3
Pod documented
247 # allows jackpot in millions or in dollars.
248 4 ▶ $jackpot = $jackpot * 10**6 if $jackpot < 10**6;
Branch: 1 branch
BranchTrueFalseTotal
if $jackpot < 1000000 3 1 4
249 4 ▶ if ( 'PowerBall' eq $game ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('PowerBall' eq $game) { } 2 2 4
elsif ('MegaMillions' eq $game) { } 2 0 2
250 2 return _round_val( _BasePBVal() + $jackpot / 292201338 );
251 }
252 elsif ( 'MegaMillions' eq $game ) {
253 2 return _round_val( _BaseMMVal() + $jackpot / 302575350 );
254 }
255 else {
256 0 die "Payout data not available for ${game}!";
257 }
258 }
259  
260 2 ▶method TicketJackPotValue ($jackpot) {
Subroutine TicketJackPotValue: called CC=4 SCAR=14.1
Pod documented
261 # allows jackpot in millions or in dollars.
262 2 ▶ $jackpot = $jackpot * 10**6 if $jackpot < 10**6;
Branch: 1 branch
BranchTrueFalseTotal
if $jackpot < 1000000 1 1 2
263 2 ▶ if ( 'PowerBall' eq $game ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('PowerBall' eq $game) { } 1 1 2
elsif ('MegaMillions' eq $game) { } 1 0 1
264 1 return _round_val( $jackpot / 292201338 );
265 }
266 elsif ( 'MegaMillions' eq $game ) {
267 1 return _round_val( $jackpot / 302575350 );
268 }
269 else {
270 0 die "Payout data not available for ${game}!";
271 }
272 }
273  
274 =head2 SavePicks
275  
276 Save an arrayref of picks from BasicDraw or BigDraw to a file.
277  
278 $game->SavePicks( $file, $picks );
279  
280 =head2 ReadPicks
281  
282 Read saved picks from a file, returns an array.
283  
284 my @picks = $game->ReadPicks( $file );
285  
286 =cut
287  
288 0 ▶method SavePicks ( $file, $picks ) {
Subroutine SavePicks: not called CC=4 SCAR=30.0
Pod documented
289 0 ▶ open( my $out, '>', $file ) || die "unable to open output $file, $?";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $file or die "unable to open output $file, $?" 0 0 0
290 0 for my $pick ( $picks->@* ) {
291 0 print $out do { join ' ', $pick->{whiteballs}->@* };
292 0 print $out ' ';
293 0 for my $red ( $pick->{redballs}->@* ) {
294 0 print $out "[${red}]";
295 }
296 0 print $out "\n";
297 }
298 }
299  
300 5 ▶method ReadPicks ($file) {
Subroutine ReadPicks: called CC=5 SCAR=16.1
Pod documented
301 5 my @picks = ();
302 5 for my $line ( path($file)->lines() ) {
303 31 ▶ next if $line =~ /^#/; # skip comment lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 5 26 31
304 26 ▶ next unless $line =~ /\d/; # skip lines missing data
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\d/u 2 24 26
305 24 my $pick = {};
306 24 chomp $line;
307 24 ▶ ( $pick->{memo}, $line ) = split /::\s+|::/, $line if ( $line =~ /::/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /::/u 4 20 24
308 24 $pick->{balls} = [ split /\s+/, $line ];
309 24 push @picks, $pick;
310 }
311 5 @picks;
312 }
313  
314 =head1 Checking Tickets
315  
316 =head2 CheckPicks
317  
318 say join "\n", $game->CheckPicks( $winnersfile, $ticketsfile );
319  
320 =head2 The Saved Tickets File Format
321  
322 This library pads single digit numbers with a leading 0. It uses [] to differentiate red balls. The balls are checked as strings so 01 and 1 are not matching numbers just as 02 and [02] are not. The entries are simply the numbers on a line, separated by spaces, with the red balls in [brackets].
323  
324 A file generated by SavePicks looks like this:
325  
326 05 40 50 57 60 [15]
327 02 33 36 44 68 [19]
328  
329 The format allows for a memo at the beginning of a line, separated from the data by a double colon ::. When included in a winners file, the memo is included in the output. Usually the memo is the date of the draw, but it can be any string. Lines beginning with # are considered comments and not read.
330  
331 Winners files need to be created by hand.
332  
333 # example winners file
334 2023-09-16:: 22 30 37 44 45 [18]
335 # the memo is just a string, it doesn't have to be the date
336 Wednesday 2023-10-11::22 24 40 52 64 [10]
337 # the memo isn't required but is helpful when you have
338 # multiple lines in your winners file.
339 16 34 46 57 59 [23]
340  
341 =cut
342  
343 1 ▶method CheckPicks ( $winnersfile, $ticketsfile ) {
Subroutine CheckPicks: called CC=7 SCAR=19.5
Pod documented
344 1 local $LIST_SEPARATOR = " ";
345 1 my @winners = $self->ReadPicks($winnersfile);
346 1 my @tickets = $self->ReadPicks($ticketsfile);
347 1 my @result = ();
348 1 for my $drawing (@winners) {
349 3 my $ctr = 1;
350 3 my $linehead = '* ';
351 3 my %winballs = map { $_ => $_ } ( $drawing->{balls}->@* );
352 3 ▶ if ( defined $drawing->{memo} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $drawing->{"memo"}) 2 1 3
353 2 $linehead .= $drawing->{memo} . ' * ';
354 }
355 3 $linehead .= "@{ $drawing->{balls} }";
356 3 push @result, $linehead;
357 3 for my $ticket (@tickets) {
358 18 my @matched = ();
359 18 my @checkballs = $ticket->{balls}->@*;
360 18 for my $ball (@checkballs) {
361 108 ▶ push @matched, ($ball) if defined $winballs{$ball};
Branch: 1 branch
BranchTrueFalseTotal
if defined $winballs{$ball} 15 93 108
362 }
363 18 my $out = " ${\ $ctr++ } -- @checkballs ";
364 18 ▶ if (@matched) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matched) 7 11 18
365 7 $out .= "matched (${\ scalar(@matched) }): @matched";
366 }
367 18 push @result, $out;
368 }
369 }
370 1 return @result;
371 }
372  
373 6 1;
374  
375 =pod
376  
377 =head1 SEE ALSO
378  
379 You can allow the Lottery to pick numbers for you, which will save you from having to put numbers you picked onto the slips for the lottery machines. If you search the web and the mobile app stores, you'll find plenty of sites and apps that do similar things to this module.
380  
381 =head1 AUTHOR
382  
383 John Karr L<BRAINBUZ|https://metacpan.org/author/BRAINBUZ>
384  
385 =head2 CONTRIBUTORS
386  
387 =head1 BUGS
388  
389 Please report any bugs or feature requests through the web interface at L<https://bitbucket.org/brainbuz/lottery/issues>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
390  
391 =head1 TODO
392  
393 The most interesting thing todo would be to use an API to get winning ticket data. There are several providers that require registering for a key. The author would prefer to use a keyless api, preferably provided by the major games or one of the state lotteries. The major games do not provide such an API.
394  
395 =head1 LICENSE AND COPYRIGHT
396  
397 Copyright 2023 John Karr.
398  
399 This program is free software: you can redistribute it and/or modify
400 it under the terms of the GNU General Public License as published by
401 the Free Software Foundation, either version 3 of the License, or
402 (at your option) any later version.
403  
404 This program is distributed in the hope that it will be useful,
405 but WITHOUT ANY WARRANTY; without even the implied warranty of
406 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
407 GNU General Public License for more details.
408  
409 You should have received a copy of the GNU General Public License
410 along with this program. If not, see L<http://www.gnu.org/licenses/>.
411  
412 =cut
« script/lottery-big-draw.pl ↑ Summary