lib/Game/Lottery.pm

filter: Statementstmt 92.4% 123 / 133 Branchbran 90.0% 36 / 40 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 95.0% 19 / 20 Podpod 100% 9 / 9 totaltotal 91.9% 193 / 210 CC 30 SCAR 16.6
CC
30
Cov
92%
SavePicks
20.0
CheckPicks
7.0
TicketValue
5.1
CRAP 5.3
↑ Summary script/lottery-big-draw.pl »
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.02
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 5 ▶use 5.038;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use experimental qw/class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 class Game::Lottery;
51 5 our $VERSION="1.02";
52 5 ▶use Math::Random::Secure;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 # use Data::Dumper;
55 # use Data::Printer;
56 5 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 5 field $game : param;
59 5 field $wb; # White Balls
60 5 field $rb; # Red Balls
61 5 field $wbd; # White Balls to Draw
62 5 field $rbd; # Red Balls to Draw
63  
64 ADJUST {
65 13 ▶ if ( $game =~ /^power/i ) {
Subroutine __ANON__: called CC=5 SCAR=16.1
Branch: 4 branches
BranchTrueFalseTotal
if ($game =~ /^power/iu) { } 5 8 13
elsif ($game =~ /^mega/iu) { } 4 4 8
elsif ($game =~ /^draw/iu) { } 1 3 4
elsif ($game =~ /^custom/iu) { } 2 1 3
66 5 $game = 'PowerBall';
67 5 $wb = 69;
68 5 $wbd = 5;
69 5 $rb = 26;
70 5 $rbd = 1;
71 }
72 elsif ( $game =~ /^mega/i ) {
73 4 $game = 'MegaMillions';
74 4 $wb = 70;
75 4 $wbd = 5;
76 4 $rb = 25;
77 4 $rbd = 1;
78 }
79 elsif ( $game =~ /^draw/i ) {
80 1 $game = 'Draw';
81 }
82 elsif ( $game =~ /^custom/i ) {
83 2 $game = 'CustomBigDraw';
84 }
85 else {
86 1 die "Unknown GAME $game.\n";
87 }
88 }
89  
90 ▶sub _PickBall ($BallLastNum) {
Subroutine _PickBall: called CC=1 SCAR=0.0
91 # rand starts at 0 -- offset the pick.
92 5043 my $pick = 1 + int Math::Random::Secure::rand($BallLastNum);
93 }
94  
95 ▶sub _DrawBalls ( $BallLastNum, $BallsDrawn ) {
Subroutine _DrawBalls: called CC=4 SCAR=13.9
96 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
97 1 die "Balls are not defined. If this is a BigDraw did you use CustomBigDrawSetup?\n";
98 }
99 11 my %balls = ();
100 11 ▶ until ( scalar( keys %balls ) == $BallsDrawn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %balls == $BallsDrawn) 43 11 54
101 43 my $newball = sprintf( "%02d", _PickBall($BallLastNum) );
102 43 $balls{$newball} = $newball;
103 }
104 11 return [ sort { $a <=> $b } ( keys(%balls) ) ];
105 }
106  
107 =head1 METHODS
108  
109 =head2 new
110  
111 Required parameter: game
112  
113 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.
114  
115 my $game = Game::Lottery->new( game => 'PowerBall');
116 my $game = Game::Lottery->new( game => 'power');
117  
118 =head2 Game
119  
120 Returns the Game.
121  
122 say $game->Game();
123  
124 =cut
125  
126 method Game {
127 2 ▶ return $game;
Subroutine Game: called CC=1 SCAR=0.0
Pod documented
128 }
129  
130 =head2 BasicDraw
131  
132 Returns an Array Reference of balls picked. Applicable to a large number of Draw games.
133  
134 my $drawn = $game->BasicDraw( $BallLastNum, $BallsDrawn );
135  
136 =cut
137  
138 ▶method BasicDraw ( $BallLastNum, $BallsDrawn ) {
Subroutine BasicDraw: called CC=1 SCAR=0.0
Pod documented
139 2 _DrawBalls( $BallLastNum, $BallsDrawn );
140 }
141  
142 =head2 BigDraw
143  
144 Draws White and Red Balls for Big Draw Games.
145  
146 Returns a Hash Reference.
147  
148 my $drawn = $game->BigDraw();
149  
150 {
151 game => $game,
152 whiteballs => ArrayRef,
153 redballs => ArrayRef
154 }
155  
156 =head2 CustomBigDrawSetup
157  
158 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
159  
160 $game->CustomBigDrawSetup(
161 game => 'Some other big draw game', # optionally set a custom game name.
162 white => 99,
163 whitecount => 1,
164 red => 20,
165 redcount => 5
166 );
167  
168 =cut
169  
170 ▶method CustomBigDrawSetup (%balls) {
Subroutine CustomBigDrawSetup: called CC=3 SCAR=11.0
Pod documented
171 2 $wb = $balls{'white'};
172 2 $wbd = $balls{'whitecount'};
173 2 $rb = $balls{'red'};
174 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
175 2 ▶ $game = $balls{'game'} if $balls{'game'};
Branch: 1 branch
BranchTrueFalseTotal
if $balls{"game"} 1 1 2
176 }
177  
178 method BigDraw {
179 5 ▶ my $draw = {
Subroutine BigDraw: called CC=1 SCAR=0.0
Pod documented
180 'game' => $game,
181 'whiteballs' => _DrawBalls( $wb, $wbd ),
182 'redballs' => _DrawBalls( $rb, $rbd )
183 };
184 4 say
185 4 qq/${\ $draw->{game} } | ${\ do { join ' ', $draw->{whiteballs}->@* } } | ${\ do { join ' ', $draw->{redballs}->@* } }/;
186 4 return $draw;
187 }
188  
189 8 ▶sub _round_val ($val) { sprintf( "%.2f", $val ) }
Subroutine _round_val: called CC=1 SCAR=0.0
190  
191 # Calculate the
192 sub _BaseMMVal {
193 3 ▶ my $val = 2 * ( 1 / 37 ); # Red
Subroutine _BaseMMVal: called CC=1 SCAR=0.0
194 3 $val += 4 * ( 1 / 89 ); # Red + 1 White
195 3 $val += 10 * ( 1 / 693 ); # Red + 2 White
196 3 $val += 10 * ( 1 / 606 ); # 3 White
197 3 $val += 200 * ( 1 / 14547 ); # Red + 3 White
198 3 $val += 500 * ( 1 / 38792 ); # 4 White
199 3 $val += 10000 * ( 1 / 931001 ); # Red + 4 White
200 3 $val += 10**6 * ( 1 / 12607306 ); # 5 White
201 3 return $val;
202 }
203  
204 sub _BasePBVal {
205 3 ▶ my $val = 4 * ( 1 / 38.32 ); # Red
Subroutine _BasePBVal: called CC=1 SCAR=0.0
206 3 $val += 4 * ( 1 / 91.98 ); # Red + 1 White
207 3 $val += 7 * ( 1 / 701.33 ); # Red + 2 White
208 3 $val += 7 * ( 1 / 579.76 ); # 3 White
209 3 $val += 100 * ( 1 / 1494.11 ); # Red + 3 White
210 3 $val += 100 * ( 1 / 36525.17 ); # 4 White
211 3 $val += 50000 * ( 1 / 913129.18 ); # Red + 4 White
212 3 $val += 10**6 * ( 1 / 11688053.52 ); # 5 White
213 3 return $val;
214 }
215  
216 =head2 TicketValue
217  
218 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.
219  
220 my $TicketValue = $game->TicketValue( 600 );
221 my $BaseTicketValue = $game->TicketValue( 0 );
222  
223 =head2 TicketJackPotValue
224  
225 Returns just value of the JackPot divided by the number of possible combinations.
226  
227 my $JackPot = $game->TicketJackPotValue( 600 );
228  
229 =cut
230  
231 4 ▶method TicketValue ( $jackpot = 0 ) {
Subroutine TicketValue: called CC=5 SCAR=16.3
Pod documented
232 # allows jackpot in millions or in dollars.
233 4 ▶ $jackpot = $jackpot * 10**6 if $jackpot < 10**6;
Branch: 1 branch
BranchTrueFalseTotal
if $jackpot < 1000000 3 1 4
234 4 ▶ if ( 'PowerBall' eq $game ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('PowerBall' eq $game) { } 2 2 4
elsif ('MegaMillions' eq $game) { } 2 0 2
235 2 return _round_val( _BasePBVal() + $jackpot / 292201338 );
236 }
237 elsif ( 'MegaMillions' eq $game ) {
238 2 return _round_val( _BaseMMVal() + $jackpot / 302575350 );
239 }
240 else {
241 0 die "Payout data not available for ${game}!";
242 }
243 }
244  
245 ▶method TicketJackPotValue ($jackpot) {
Subroutine TicketJackPotValue: called CC=4 SCAR=14.1
Pod documented
246 # allows jackpot in millions or in dollars.
247 2 ▶ $jackpot = $jackpot * 10**6 if $jackpot < 10**6;
Branch: 1 branch
BranchTrueFalseTotal
if $jackpot < 1000000 1 1 2
248 2 ▶ if ( 'PowerBall' eq $game ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('PowerBall' eq $game) { } 1 1 2
elsif ('MegaMillions' eq $game) { } 1 0 1
249 1 return _round_val( $jackpot / 292201338 );
250 }
251 elsif ( 'MegaMillions' eq $game ) {
252 1 return _round_val( $jackpot / 302575350 );
253 }
254 else {
255 0 die "Payout data not available for ${game}!";
256 }
257 }
258  
259 =head2 SavePicks
260  
261 Save an arrayref of picks from BasicDraw or BigDraw to a file.
262  
263 $game->SavePicks( $file, $picks );
264  
265 =head2 ReadPicks
266  
267 Read saved picks from a file, returns an array.
268  
269 my @picks = $game->ReadPicks( $file );
270  
271 =cut
272  
273 ▶method SavePicks ( $file, $picks ) {
Subroutine SavePicks: not called CC=4 SCAR=30.0
Pod documented
274 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
275 0 for my $pick ( $picks->@* ) {
276 0 print $out do { join ' ', $pick->{whiteballs}->@* };
277 0 print $out ' ';
278 0 for my $red ( $pick->{redballs}->@* ) {
279 0 print $out "[${red}]";
280 }
281 0 print $out "\n";
282 }
283 }
284  
285 ▶method ReadPicks ($file) {
Subroutine ReadPicks: called CC=5 SCAR=16.1
Pod documented
286 5 my @picks = ();
287 5 for my $line ( path($file)->lines() ) {
288 31 ▶ next if $line =~ /^#/; # skip comment lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 5 26 31
289 26 ▶ next unless $line =~ /\d/; # skip lines missing data
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\d/u 2 24 26
290 24 my $pick = {};
291 24 chomp $line;
292 24 ▶ ( $pick->{memo}, $line ) = split /::\s+|::/, $line if ( $line =~ /::/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /::/u 4 20 24
293 24 $pick->{balls} = [ split /\s+/, $line ];
294 24 push @picks, $pick;
295 }
296 5 @picks;
297 }
298  
299 =head1 Checking Tickets
300  
301 =head2 CheckPicks
302  
303 say join "\n", $game->CheckPicks( $winnersfile, $ticketsfile );
304  
305 =head2 The Saved Tickets File Format
306  
307 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].
308  
309 A file generated by SavePicks looks like this:
310  
311 05 40 50 57 60 [15]
312 02 33 36 44 68 [19]
313  
314 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.
315  
316 Winners files need to be created by hand.
317  
318 # example winners file
319 2023-09-16:: 22 30 37 44 45 [18]
320 # the memo is just a string, it doesn't have to be the date
321 Wednesday 2023-10-11::22 24 40 52 64 [10]
322 # the memo isn't required but is helpful when you have
323 # multiple lines in your winners file.
324 16 34 46 57 59 [23]
325  
326 =cut
327  
328 ▶method CheckPicks ( $winnersfile, $ticketsfile ) {
Subroutine CheckPicks: called CC=7 SCAR=19.5
Pod documented
329 1 local $LIST_SEPARATOR = " ";
330 1 my @winners = $self->ReadPicks($winnersfile);
331 1 my @tickets = $self->ReadPicks($ticketsfile);
332 1 my @result = ();
333 1 for my $drawing (@winners) {
334 3 my $ctr = 1;
335 3 my $linehead = '* ';
336 3 my %winballs = map { $_ => $_ } ( $drawing->{balls}->@* );
337 3 ▶ if ( defined $drawing->{memo} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $drawing->{"memo"}) 2 1 3
338 2 $linehead .= $drawing->{memo} . ' * ';
339 }
340 3 $linehead .= "@{ $drawing->{balls} }";
341 3 push @result, $linehead;
342 3 for my $ticket (@tickets) {
343 18 my @matched = ();
344 18 my @checkballs = $ticket->{balls}->@*;
345 18 for my $ball (@checkballs) {
346 108 ▶ push @matched, ($ball) if defined $winballs{$ball};
Branch: 1 branch
BranchTrueFalseTotal
if defined $winballs{$ball} 15 93 108
347 }
348 18 my $out = " ${\ $ctr++ } -- @checkballs ";
349 18 ▶ if (@matched) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matched) 7 11 18
350 7 $out .= "matched (${\ scalar(@matched) }): @matched";
351 }
352 18 push @result, $out;
353 }
354 }
355 1 return @result;
356 }
357  
358 5 1;
359  
360 =pod
361  
362 =head1 SEE ALSO
363  
364 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.
365  
366 =head1 AUTHOR
367  
368 John Karr L<BRAINBUZ|https://metacpan.org/author/BRAINBUZ>
369  
370 =head2 CONTRIBUTORS
371  
372 =head1 BUGS
373  
374 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.
375  
376 =head1 TODO
377  
378 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.
379  
380 =head1 LICENSE AND COPYRIGHT
381  
382 Copyright 2023 John Karr.
383  
384 This program is free software: you can redistribute it and/or modify
385 it under the terms of the GNU General Public License as published by
386 the Free Software Foundation, either version 3 of the License, or
387 (at your option) any later version.
388  
389 This program is distributed in the hope that it will be useful,
390 but WITHOUT ANY WARRANTY; without even the implied warranty of
391 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
392 GNU General Public License for more details.
393  
394 You should have received a copy of the GNU General Public License
395 along with this program. If not, see L<http://www.gnu.org/licenses/>.
396  
397 =cut
↑ Summary script/lottery-big-draw.pl »