Creating a Reusable Lottery Number Generator Function
Now that we understand how to generate unique random numbers, let's create a reusable function for our lottery number generator. This will make our code more organized and allow us to easily generate numbers for different lottery formats.
Creating a Functions File
Let's create a new file with our lottery functions:
- In the WebIDE, navigate to the
~/project/lottery
directory
- Create a new file called
lottery_functions.py
Add the following code to define our lottery number generator function:
import random
def generate_lottery_numbers(count, min_num, max_num):
"""
Generate a specified count of unique random numbers within a given range.
Args:
count (int): Number of unique numbers to generate
min_num (int): Minimum value (inclusive)
max_num (int): Maximum value (inclusive)
Returns:
list: Sorted list of unique random numbers
"""
## Validate inputs
if count > (max_num - min_num + 1):
raise ValueError(f"Cannot generate {count} unique numbers in range {min_num}-{max_num}")
## Generate unique random numbers
numbers = random.sample(range(min_num, max_num + 1), count)
## Sort the numbers
numbers.sort()
return numbers
def generate_powerball_numbers():
"""
Generate numbers for Powerball lottery (5 numbers from 1-69 and 1 from 1-26).
Returns:
tuple: (list of main numbers, powerball number)
"""
main_numbers = generate_lottery_numbers(5, 1, 69)
powerball = random.randint(1, 26)
return (main_numbers, powerball)
def generate_mega_millions_numbers():
"""
Generate numbers for Mega Millions lottery (5 numbers from 1-70 and 1 from 1-25).
Returns:
tuple: (list of main numbers, mega ball number)
"""
main_numbers = generate_lottery_numbers(5, 1, 70)
mega_ball = random.randint(1, 25)
return (main_numbers, mega_ball)
Now, let's create a file to test our functions:
- In the WebIDE, create a new file called
test_lottery_functions.py
Add the following code to test our functions:
import lottery_functions
## Test standard lottery function (e.g., 6 numbers from a range of 1-49)
standard_lottery = lottery_functions.generate_lottery_numbers(6, 1, 49)
print(f"Standard lottery (6 from 1-49): {standard_lottery}")
## Test Powerball function
main_numbers, powerball = lottery_functions.generate_powerball_numbers()
print(f"Powerball: Main numbers: {main_numbers}, Powerball: {powerball}")
## Test Mega Millions function
main_numbers, mega_ball = lottery_functions.generate_mega_millions_numbers()
print(f"Mega Millions: Main numbers: {main_numbers}, Mega Ball: {mega_ball}")
## Test with different parameters
custom_lottery = lottery_functions.generate_lottery_numbers(4, 1, 20)
print(f"Custom lottery (4 from 1-20): {custom_lottery}")
## Test error handling - Try to generate too many numbers
try:
## Trying to get 10 numbers from a range of only 5 numbers (impossible)
impossible_lottery = lottery_functions.generate_lottery_numbers(10, 1, 5)
except ValueError as e:
print(f"Error caught successfully: {e}")
Run the test file to see our functions in action:
cd ~/project/lottery
python3 test_lottery_functions.py
You should see output similar to:
Standard lottery (6 from 1-49): [4, 17, 23, 26, 39, 48]
Powerball: Main numbers: [3, 18, 27, 42, 61], Powerball: 13
Mega Millions: Main numbers: [7, 24, 31, 52, 67], Mega Ball: 9
Custom lottery (4 from 1-20): [2, 9, 15, 19]
Error caught successfully: Cannot generate 10 unique numbers in range 1-5
Benefits of Using Functions
By creating these reusable functions, we've achieved several important programming goals:
- Code Reusability: We can generate lottery numbers anywhere in our program without duplicating code
- Input Validation: Our function checks if the requested number of unique values is possible in the given range
- Abstraction: We've hidden the implementation details inside functions with descriptive names
- Specialized Functions: We've created specific functions for common lottery formats
This modular approach makes our code more maintainable and easier to understand. In the next step, we'll use these functions to create a complete lottery application with a user interface.