Random Number Generation - Popcorn Hacks & Homework

Popcorn Hack 1: Real-World Applications

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

Answer:

  1. Cybersecurity: Random number generation is used to create encryption keys that secure sensitive data in digital communication. If the keys were predictable, attackers could easily break the encryption.

  2. Gaming: In games, random numbers are used for dice rolls, loot drops, and procedural content generation, making each playthrough unique and engaging.


Popcorn Hack 2: Magic 8-Ball:

Task: Complete the magic_8_ball function that:

  • Returns “Yes” roughly 50% of the time
  • Returns “No” roughly 25% of the time
  • Returns “Ask again later” roughly 25% of the time

Modified Code:

import random

def magic_8_ball():
    roll = random.randint(1, 4)
    if roll <= 2:
        return "Yes"         # 50%
    elif roll == 3:
        return "No"          # 25%
    else:
        return "Ask again later"  # 25%

# Test
for i in range(10):
    print(f"Magic 8-Ball says: {magic_8_ball()}")
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: Ask again later
Magic 8-Ball says: No
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: No
Magic 8-Ball says: Yes

Popcorn Hack 3: Traffic Light Simulation Modification

Task: Modify the simulation so that:

  • Green lasts for 5 steps
  • Yellow lasts for 2 steps
  • Red lasts for 4 steps
  • Run it for 20 time steps and print the results

🧠 Reflection

This is a simulation because it models the behavior of a real-world system—traffic lights—using a sequence of timed states. It helps us predict how traffic flows and how long each signal remains active, which is useful for traffic engineering and urban planning to reduce congestion and improve safety.

Modified Code:

states = ["Green", "Yellow", "Red"]
durations = {"Green": 5, "Yellow": 2, "Red": 4}
timeline = []

# Simulate 20 time steps
time = 0
state = "Green"
counter = 0

while time < 20:
    timeline.append((time, state))
    counter += 1
    if counter == durations[state]:
        counter = 0
        current_index = states.index(state)
        state = states[(current_index + 1) % len(states)]
    time += 1

for t, s in timeline:
    print(f"Time {t}: {s}")

Time 0: Green
Time 1: Green
Time 2: Green
Time 3: Green
Time 4: Green
Time 5: Yellow
Time 6: Yellow
Time 7: Red
Time 8: Red
Time 9: Red
Time 10: Red
Time 11: Green
Time 12: Green
Time 13: Green
Time 14: Green
Time 15: Green
Time 16: Yellow
Time 17: Yellow
Time 18: Red
Time 19: Red

Homework Hack 1: Dice Game (Simulation and Randomness)

import random

def roll_dice():
    die1 = random.randint(1, 6)
    die2 = random.randint(1, 6)
    return die1, die2, die1 + die2

def play_dice_game():
    die1, die2, total = roll_dice()
    print(f"Initial roll: {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"Point is set to {point}")
        while True:
            die1, die2, total = roll_dice()
            print(f"Rolling again: {die1} + {die2} = {total}")
            if total == point:
                print("You hit your point again! You win!")
                return True
            elif total == 7:
                print("You rolled a 7. You lose!")
                return False

def main():
    wins = 0
    losses = 0

    while True:
        play = input("Do you want to play the dice game? (yes/no): ").lower()
        if play == "yes":
            if play_dice_game():
                wins += 1
            else:
                losses += 1
            print(f"Wins: {wins}, Losses: {losses}")
        else:
            print("Game over!")
            print(f"Final Stats - Wins: {wins}, Losses: {losses}")
            break

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

Welcome to the Dice Game!
Initial roll: 3 + 3 = 6
Point is set to 6
Rolling again: 2 + 6 = 8
Rolling again: 5 + 4 = 9
Rolling again: 5 + 2 = 7
You rolled a 7. You lose!
Wins: 0, Losses: 1
Initial roll: 6 + 5 = 11
You win!
Wins: 1, Losses: 1
Initial roll: 5 + 6 = 11
You win!
Wins: 2, Losses: 1
Initial roll: 1 + 5 = 6
Point is set to 6
Rolling again: 1 + 3 = 4
Rolling again: 5 + 3 = 8
Rolling again: 6 + 2 = 8
Rolling again: 1 + 2 = 3
Rolling again: 5 + 2 = 7
You rolled a 7. You lose!
Wins: 2, Losses: 2
Initial roll: 5 + 4 = 9
Point is set to 9
Rolling again: 6 + 1 = 7
You rolled a 7. You lose!
Wins: 2, Losses: 3
Initial roll: 1 + 6 = 7
You win!
Wins: 3, Losses: 3
Initial roll: 1 + 1 = 2
You lose!
Wins: 3, Losses: 4
Initial roll: 2 + 3 = 5
Point is set to 5
Rolling again: 5 + 6 = 11
Rolling again: 6 + 1 = 7
You rolled a 7. You lose!
Wins: 3, Losses: 5
Game over!
Final Stats - Wins: 3, Losses: 5