Wednesday, May 1, 2013

Quick lottery program

While I was performing after hours database work for my day job, I figured I would build a simplistic java code that would do text input/output for two major lottery games (Power Ball and Mega Millions).

Disclaimer: The following information is provided for entertainment and educational puposes. If you do not win the Power Ball or Mega Millions after utilizing, do not blame the author. If you win using the code, you can donate to a charity in my name.

To generate the code you need to first read the rules for the lottery games. You can find the general rules for the Power Ball at this link and the rules for the Mega Millions at this link. Once you have a quick understanding of the rules, you can get create the following rules:
  1. Each game has a random selection of balls (5) that could be any number from 1 to max balls set for that game. Each of the balls have to be unique from each other.
  2. The special ball (power ball or mega ball) can be any number from 1 to the max balls set for the special ball.
Based off the rules presented above, you can develop the following algorithm.
  1. Present Game Selection Menu (Power Ball or Mega Millions) and take user input
  2. Get user input on the number of quick pick  results to generate
  3. Randomly select values for game selected
To break down the random selection of balls for the games there are only a few things you need to account for:
  1. Get the value of the first ball and make sure it does not equal 0. This is because most random generators will give you a random value between 0 and the max value. Any ball after after the first can not equal the value of the other balls selected or 0.
  2. Get the value of the special ball and make sure that it does not equal 0.
That is it. Below is a snapshot of what the code could look like for the specific game of Power Ball
Code Snip of Power Ball selection

Here is the link to my code in the event you would like to play around with it.

Output Snip of Lottery program


Please enjoy.
Syd

No comments:

Post a Comment