Here's a Python program for a random number generator with multiple features:
import random
import sys
def simple_random():
"""Generate a simple random number between 1 and 100"""
return random.randint(1, 100)
def custom_range_random(min_val, max_val):
"""Generate a random number within a custom range"""
return random.randint(min_val, max_val)
def random_float(min_val=0.0, max_val=1.0):
"""Generate a random floating-point number"""
return random.uniform(min_val, max_val)
def random_choice_from_list(items):
"""Pick a random item from a list"""
return random.choice(items)
def multiple_random_numbers(count, min_val=1, max_val=100):
"""Generate multiple random numbers"""
return [random.randint(min_val, max_val) for _ in range(count)]
def weighted_random():
"""Generate a weighted random choice"""
choices = ['common', 'uncommon', 'rare', 'legendary']
weights = [50, 30, 15, 5] # Higher numbers = more likely
return random.choices(choices, weights=weights)[0]
def main():
print("=== Random Number Generator ===\n")
while True:
print("Choose an option:")
print("1. Simple random number (1-100)")
print("2. Custom range random number")
print("3. Random decimal number")
print("4. Random choice from list")
print("5. Multiple random numbers")
print("6. Weighted random choice")
print("7. Exit")
try:
choice = int(input("\nEnter your choice (1-7): "))
if choice == 1:
result = simple_random()
print(f"Random number: {result}\n")
elif choice == 2:
min_val = int(input("Enter minimum value: "))
max_val = int(input("Enter maximum value: "))
result = custom_range_random(min_val, max_val)
print(f"Random number between {min_val} and {max_val}: {result}\n")
elif choice == 3:
min_val = float(input("Enter minimum value: "))
max_val = float(input("Enter maximum value: "))
result = random_float(min_val, max_val)
print(f"Random decimal: {result:.4f}\n")
elif choice == 4:
items_input = input("Enter items separated by commas: ")
items = [item.strip() for item in items_input.split(',')]
result = random_choice_from_list(items)
print(f"Random choice: {result}\n")
elif choice == 5:
count = int(input("How many numbers? "))
min_val = int(input("Enter minimum value: "))
max_val = int(input("Enter maximum value: "))
results = multiple_random_numbers(count, min_val, max_val)
print(f"Random numbers: {results}\n")
elif choice == 6:
result = weighted_random()
print(f"Weighted random choice: {result}\n")
elif choice == 7:
print("Goodbye!")
sys.exit()
else:
print("Invalid choice. Please enter 1-7.\n")
except ValueError:
print("Please enter a valid number.\n")
except KeyboardInterrupt:
print("\nGoodbye!")
sys.exit()
if __name__ == "__main__":
# Set a seed for reproducible results (optional)
# random.seed(42)
main()
This program includes several types of random number generation:
Features:
- Simple random integers between 1-100
- Custom range random numbers
- Random floating-point numbers
- Random selection from user-provided lists
- Multiple random numbers at once
- Weighted random choices (some outcomes more likely than others)
- Interactive menu system
- Error handling for invalid inputs
Key Functions Used:
random.randint(a, b) - Random integer between a and b (inclusive)
random.uniform(a, b) - Random float between a and b
random.choice(sequence) - Random item from a sequence
random.choices(population, weights) - Weighted random selection
Usage Example:
# Quick usage without the menu
print(random.randint(1, 10)) # Random int 1-10
print(random.uniform(0, 1)) # Random float 0-1
print(random.choice(['A', 'B', 'C'])) # Random choice
You can uncomment the random.seed(42) line if you want reproducible results for testing purposes.