Skip to the content.

Random Numbers

04.11.2025

Popcorn Hack 1: Real-World Applications

Question: Name two real-world applications where random number generation is essential and briefly explain why.

1. Cybersecurity (Encryption):
Random number generation is crucial for creating secure encryption keys. If the numbers weren’t truly random, hackers could predict them and break into secure systems.

2. Video Games:
Games use random numbers to generate unpredictable events, like loot drops or enemy behavior. This keeps gameplay dynamic and different each time you play.

import random

def magic_8_ball():
    rand = random.random()  # generates a number between 0.0 and 1.0

    if rand < 0.5:
        return "Yes"
    elif rand < 0.75:
        return "No"
    else:
        return "Ask again later"

# Test your function
for i in range(10):
    print(f"Magic 8-Ball says: {magic_8_ball()}")

def traffic_light_simulation():
    steps = 20
    for i in range(steps):
        cycle_position = i % (5 + 2 + 4)  # Total cycle = 11 steps

        if cycle_position < 5:
            light = "Green"
        elif cycle_position < 7:
            light = "Yellow"
        else:
            light = "Red"

        print(f"Step {i + 1}: {light}")

traffic_light_simulation()

Popcorn Hack 3 Reflection

This is a simulation because it models the behavior of a real traffic light using step-by-step logic in code. It helps us visualize how timing and sequence control traffic flow, and it can be used in real-world scenarios to design safer and more efficient intersections.

import random

def roll_dice():
    """Roll two dice and return their values and sum."""
    die1 = random.randint(1, 6)
    die2 = random.randint(1, 6)
    total = die1 + die2
    return die1, die2, total

def play_dice_game():
    """
    Play one round of the dice game.
    Returns True if player wins, False if player loses.
    """
    die1, die2, total = roll_dice()
    print(f"You rolled: {die1} + {die2} = {total}")

    if total in [7, 11]:
        print("You win!")
        return True
    elif total in [2, 3, 12]:
        print("You lose!")
        return False
    else:
        point = total
        print(f"Your point is {point}. Keep rolling!")

        while True:
            die1, die2, total = roll_dice()
            print(f"You rolled: {die1} + {die2} = {total}")

            if total == point:
                print("You rolled your point again. You win!")
                return True
            elif total == 7:
                print("You rolled a 7. You lose!")
                return False

def main():
    """Main game function with game loop and statistics."""
    wins = 0
    losses = 0

    while True:
        play = input("Do you want to play? (yes/no): ").lower()
        if play != "yes":
            break

        if play_dice_game():
            wins += 1
        else:
            losses += 1

        print(f"Current Stats -> Wins: {wins}, Losses: {losses}\n")

    print("Thanks for playing!")
    print(f"Final Stats -> Wins: {wins}, Losses: {losses}")

if __name__ == "__main__":
    print("Welcome to the Dice Game!")
    main()