# Lottery_Project A Python project to analyze and pick lottery numbers using past draw results to predict future draws. ## Usage ### Top Level ```bash usage: main.py [-h] {update,record-count,generate-ticket,predict} ... A lottery prediction tool. positional arguments: {update,record-count,generate-ticket,predict} update Update database records record-count Get record counts by table in the database generate-ticket Generate a random ticket for the specified game predict Predict the next drawing options: -h, --help show this help message and exit ``` ### Update Scrape the game data from the source site and insert records into the database that are not present. A specific year can be selected as the starting year. ```bash usage: main.py update [-h] [-y YEAR] [-d] options: -h, --help show this help message and exit -y, --year YEAR Year to start updates with -d, --dryrun Run the update process without actually making any database or API calls ``` ### Record Count Return a count of records in each game table. A date to start the count from can be specified. ```bash usage: main.py record-count [-h] [-f FROM_DATE] options: -h, --help show this help message and exit -f, --from-date FROM_DATE Date from which to base the count on (ex. 2026-01-01). The default value is rule-change for the date when game rules last changed ``` ### Generate Ticket Generates a random ticket for the game specified. ```bash usage: main.py generate-ticket [-h] --game {MegaMillions,Powerball} options: -h, --help show this help message and exit --game {MegaMillions,Powerball} The game to predict the next result for ``` ### Predict Due to the lottery drawings being as randomized as possible, generating an accurate prediction is not possible. Many window sizes have been tried during testing and none of them have yielded any hopeful results. The same numbers will most likely be generated with the same window size until the sample changes enough to cause changes in the selections. A different random seed number is chosen for each run to attempt to combat this behavior. cause changes in the selections. A different random seed number is chosen for each run to attempt to combat this behavior. ```bash usage: main.py predict [-h] --game {MegaMillions,Powerball} [--window-size WINDOW_SIZE] [--test] options: -h, --help show this help message and exit --game {MegaMillions,Powerball} The game to predict the next result for --window-size WINDOW_SIZE The number of records to use in the prediction model, defaults to 10, use -1 for all records --test Use the most recent drawing as a test subject to find the right window_size ``` ## Draw Dates Mega Millions drawings are held every Tuesday and Friday. Powerball drawings are held every Monday, Wednesday, and Friday. In order to conserve processing cycles, the predictor will only request new data if there is the potential for new drawings to collect and save. Powerball can have up to 156/157 drawings and Mega Millions can have up to 104/105 drawings per year, depending on how draw dates fall. ## Storage The data will eventually be stored in database tables for faster execution. This will also allow for dynamic calls for more data based on the draw dates and last drawing stored locally. ### Database Engine Sqlite has been chosen for portability and ease of use. ## Historical Info The first Powerball drawing was on April 22, 1992. The first Mega Millions drawing was on September 6, 1996. The original name was "The Big Game" and was rebranded to Mega Millions in May 2002. However, each game has undergone rule changes that have altered the pool of numbers available for the main (white) balls and their respective game balls as well. Due to this, the historical information used to analyze the draws should start at the most recent rule change date. **The most recent change for Powerball was on October 4, 2015, when the main ball pool increased from 59 to 69 numbers and the Powerball pool reduced from 35 to 26 numbers.** **The most recent change for Mega Millions was on April 5, 2025, when the Megaball ball pool was decreased from 25 to 24 numbers as well as the addition of a 10x multiplier. The existing pool of 70 numbers for the main game ball pool was part of changes made on October 28, 2017.** Historical results can be scraped from the web and added to the database from the URLs below. The results must be scraped by year. Changing the year in the URLs alters the resultset displayed. Powerball: https://www.lotto.net/powerball/numbers/1992 Mega Millions: https://www.lotto.net/mega-millions/numbers/1996 ### Historical Information Sources - [Mega Millions Wikipedia](https://en.wikipedia.org/wiki/Mega_Millions) - [Powerball Wikipedia](https://en.wikipedia.org/wiki/Powerball) ## Environment Variables To facilitate environment variables for testing or execution, create a .env file in the root folder of the project and add the following lines to it: ```bash # General vars LP_BASE_SCRAPE_URL="https://www.lotto.net/" LP_RESULT_CONTAINER="div" LP_RESULT_CLASS="results-vsmall archive-list" LP_DRAW_DATE_CONTAINER="div" LP_DRAW_DATE_CLASS="date" LP_BALL_CONTAINER='li' LP_BALL_CLASS="ball ball" LP_DATE_INPUT_FORMAT="%B %d %Y" LP_DATE_PASS_FORMAT="%m/%d/%Y" LP_JACKPOT_CONTAINER="div" LP_BAR_FORMAT="{l_bar}{bar:30}{r_bar}" # Powerball vars LP_POWERBALL_INCEPTION="1992" LP_POWERBALL_CLASS="ball powerball" LP_POWERBALL_MULTIPLIER_CLASS="ball power-play" LP_POWERBALL_RULE_CHANGE_DATE="2015-10-04" LP_POWERBALL_MAX_MAIN_BALL="69" LP_POWERBALL_MAX_POWERBALL="26" # Mega Millions vars LP_MEGA_MILLIONS_INCEPTION="1996" LP_MEGABALL_CLASS="ball mega-ball" LP_MEGA_MILLIONS_MULTIPLIER_CLASS="ball megaplier" LP_MEGA_MILLIONS_RULE_CHANGE_DATE="2025-04-05" LP_MEGA_MILLIONS_MAX_MAIN_BALL="70" LP_MEGA_MILLIONS_MAX_MEGA_BALL="24" # RegEx vars LP_DATE_CLEAN_REGEX="r'(st|nd|rd|th)'" # Datbase vars LP_DATABASE_URL="sqlite:///predictor.db" LP_DATE_INSERT_FORMAT="%Y-%m-%d" # For pytest clean output PYTHONWARNINGS=ignore ``` ## TO-DOs 1. ~~Review standard tests for sqlalchemy objects~~ 2. ~~Decide which DBE to use for data storage~~ 3. ~~Create tests with in-memory database~~ 4. ~~Update local database schema (alembic)~~ 5. Complete starter code review 6. ~~Add code to analyze numbers~~ 7. ~~Add code to main.py~~