Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Random Generator to Hit the Powerball Jackpot

DZone's Guide to

Using Random Generator to Hit the Powerball Jackpot

As we continue to explore weights and randomness, see how an open source library tackles winning the biggest prize of them all: the lottery.

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

Recently, I completed a series on an open source library I created called Random Generator. Upon completion of the series, I wrote another article focused on using Random Generator to correctly pick the teams for the NCAA Men's Basketball tournament, using results and information from the 2015-2016 season. If you want to read my series on Random Generator, below are links to each article in the series:

As part of my daily commute, I drive past one of those lottery signs that provides an update for the latest estimated payouts for various lottery games available in my area. The biggest lottery in the Midwest is the Powerball. I remember there being a couple really large payouts last year, so I wondered if Random Generator could pick the winner number for one of those drawings.

How Powerball Works

The Powerball drawing randomly picks five numbers out of a list of 69 choices. Then a single Powerball is picked, which ranges from 1 to 26. If you are lucky enough to pick all six numbers, you win the jackpot. The odds of winning the jackpot are 1 in 292,201,388.

According to an article published by CNN Money, you have better odds for the following items to happen:

  • Give birth to quadruplets (1 in 729,000)

  • Be killed by an asteroid strike (1 in 700,000)

  • Be killed by a lightning strike (1 in 164,968)

NBC News provided some less morbid comparisons in this article:

  • Being drafted into the NBA after college (1 in 6,864,000)

  • Dating Actor Brad Pitt (1 in 1,505,000)

  • Becoming the US president (1 in 10,000,000)

  • Making a hole in one on a par 3 hole as an amateur golfer (1 in 12,500)

The Powerball Jackpot

Looking at the history of the Powerball drawings, I decided to focus on the November 27, 2016 Powerball drawing, which had escalated to a winning prize of $420.9 million (US). The winning numbers are listed below:

17 19 21 37 44 and the Powerball was 16

That drawing had only a single winner, which had an estimated cash value of $254.6 million (US) and the winning ticket was purchased in the state of Tennessee.

Random Generator

Since Random Generator was designed to randomize elements of a java.util.List and can be configured to return a subset of the list, I decided to see if Random Generator could correctly pick the winning numbers from the November 27th drawing. First, I created two simple methods to build a list of java.lang.Integer objects. One for the main range of numbers:

private static ArrayList<Integer> getMainNumbers() {
    ArrayList<Integer> returnList = new ArrayList<Integer>();
    returnList.add(new Integer("1"));
    returnList.add(new Integer("2"));
    ...
    returnList.add(new Integer("68"));
    returnList.add(new Integer("69"));
    return returnList;
}


One for the Powerball range of numbers:

private static ArrayList<Integer> getPowerballNumbers() {
    ArrayList<Integer> returnList = new ArrayList<Integer>();
    returnList.add(new Integer("1"));
    returnList.add(new Integer("2"));
    ...
    returnList.add(new Integer("25"));
    returnList.add(new Integer("26"));
    return returnList;
}


With these methods in place, it was easy to use Random Generator to provide a list of Powerball picks:

RandomGenerator randomGenerator = new RandomGenerator();
List<Integer> myPicks = randomGenerator.randomize(getMainNumbers(), new Integer("5"));
List<Integer> myPowerball = randomGenerator.randomize(getPowerballNumbers(), new Integer("1"));


From the Powerball odds page, I realize that picking one set of Powerball picks has very low odds at matching the winning numbers, so I updated the code in a for loop so that I could repeat the process multiple times:

for (int i = 0; i < loopCount; i++) {
    List<Integer> myPicks = randomGenerator.randomize(getMainNumbers(), new Integer("5"));
    List<Integer> myPowerball = randomGenerator.randomize(getPowerballNumbers(), new Integer("1"));

    // processing logic occurs here ...
}


I added processing logic to keep track of the number of times each number, which was part of the winning set, was found. I then kept track of the number of times matches that provide a payout (e.g. two numbers and the Powerball) were encountered as well.

With the code in place, I set loopCount to 10 million iterations. While Random Generator did not correctly pick all the numbers correctly, it did yield the following results:

Found winning number 17 = 723,271 time(s)
Found winning number 19 = 723,596 time(s)
Found winning number 21 = 724,907 time(s)
Found winning number 37 = 724,610 time(s)
Found winning number 44 = 723,373 time(s)
Found Powerball (16) = 384,708 time(s)

Instances with 2 correct numbers = 370,272
Instances with 3 correct numbers = 18,013
Instances with 4 correct numbers = 289
Instances with 5 correct numbers = 2
Instances where all numbers were found, except the Powerball = 2
Tickets with zero matches = 6,526,799

Based upon the current Powerball payouts, the following winnings would be applied, based upon 10 million iterations:

Powerball (only) Match = $1,042,468.00
1 + Powerball Match = $436,400.00
2 + Powerball Match = $100,037.00
3 Matches (no Powerball) = $126,091.00
3 + Powerball Match = $69,100.00
4 Matches (no Powerball) = $28,900.00
4 + Powerball Match = $450,000.00
5 Matches (no Powerball) = $2,000,000.00
Grand Prize = $0.00
Total Prize Money = $4,252,996.00
Ticket Costs - Total Prizes = ($15,747,004.00)

In the end, an investment of $20 million in Powerball tickets led to a return of only $4.3 million, or a Return on Investment (ROI) of -79%. Of course, hitting the jackpot one time, is all that it would have taken to make the investment worthwhile.

Updates to lotterypick

The code I used for the test above has been added to my GitLab account and can be found at the following URL:

https://gitlab.com/johnjvester/lotterypick

Buying 10,000 million quick picks (random picks) does not guarantee duplicates from being purchase. However, I have been working on updates to employ the use of a java.util.HashSet to make sure all processed picks are unique. In introducing this logic, I have been dealing with increased memory usage and performance when processing over 40 million unique picks.

You are welcome to download or fork this repository. Be warned, the code was for exploratory purposes and is far from the best code I have ever written.

Have a really great day!

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,randomness ,how to articles ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}