Random Generator Functions for Various Data Types

  • Share this:

Code introduction


This script includes multiple random generation functions such as dates, emails, phone numbers, colors, facts, quotes, and greetings.


Technology Stack : random, datetime, fire

Code Type : Python Script

Code Difficulty : Intermediate


                
                    
import random
import datetime
import fire

def generate_random_date(start_date, end_date):
    """
    Generate a random date between start_date and end_date (inclusive).
    """
    delta = end_date - start_date
    random_seconds = random.randrange(delta.total_seconds())
    return start_date + datetime.timedelta(seconds=random_seconds)

def generate_random_email():
    """
    Generate a random email address.
    """
    domain = "example.com"
    return random.choice(["user", "admin", "support", "info"]) + "@" + domain

def generate_random_phone_number():
    """
    Generate a random phone number.
    """
    return random.choice(["+1", "+44", "+91"]) + " " + "".join(random.choices("0123456789", k=10))

def generate_random_color():
    """
    Generate a random color as a hex string.
    """
    return f"#{random.randint(0, 0xFFFFFF):06x}"

def generate_random_fact():
    """
    Generate a random fact.
    """
    facts = [
        "The Great Wall of China is visible from space.",
        "Peanuts are not nuts; they are legumes.",
        "A group of geese on a river is called a 'gaggle'.",
        "The shortest war in history was between England and Zanzibar in 1896, lasting only 38 minutes."
    ]
    return random.choice(facts)

def generate_random_quote():
    """
    Generate a random quote.
    """
    quotes = [
        "The only way to do great work is to love what you do. - Steve Jobs",
        "Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill",
        "The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt",
        "The best way to predict the future is to create it. - Peter Drucker"
    ]
    return random.choice(quotes)

def generate_random_greeting():
    """
    Generate a random greeting.
    """
    greetings = [
        "Good morning!",
        "Hello there!",
        "Hi!",
        "Hey!",
        "Good afternoon!",
        "Good evening!"
    ]
    return random.choice(greetings)

def generate_random_fact_and_quote():
    """
    Generate a random fact and quote.
    """
    return generate_random_fact(), generate_random_quote()

# Usage example:
# python your_script.py generate_random_fact_and_quote