Skip to the content.

Simulation Lesson

popcorn/homework hacks

POPCORN HACKS

Popcorn hack 1

Generating a Password on a Website: Websites use random number generators to create strong, unpredictable passwords that are harder for hackers to guess, helping keep user accounts secure. This generates random numbers/charecters at the push of a button.

Rolling Dice in a Game: Rolling dice in a game is random because each side has the same statistical likleyhood of being rolled on (without outside inputs). This ensures fairness to board games/digital games making each playthrough diffrent.

Popcorn hack 2

import random

def magic_8_ball():
    num = random.random()  # Generates a number between 0.0 and 1.0
    if num < 0.5:
        return "Yes"
    elif num < 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()}")

Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: No
Magic 8-Ball says: Yes
Magic 8-Ball says: No
Magic 8-Ball says: Yes
Magic 8-Ball says: No
Magic 8-Ball says: No
Magic 8-Ball says: Yes
Magic 8-Ball says: Ask again later

Popcorn hack 3

# Traffic light simulation (no randomness)

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

Explination: This is a simulation because it models the behavior of a real-world system (a traffic light) using rules/logic to deduce when to turn the light red, green, or yellow. Its real-world impact is in helping design safer and more efficient traffic flow patterns, reducing accidents and congestion at intersections.

Percent: Green lasts for 50%, Red: 30%, and Yellow: 20%


HOMEWORK HACKS

Hack 1

import random

def roll_dice():
    """Roll two dice and return their values and sum."""
    die1 = random.randint(1, 6) # rolling 2 dice
    die2 = random.randint(1, 6)
    total = die1 + die2 # Totaling the values
    print(f"You rolled {die1} + {die2} = {total}")
    return total

def play_dice_game():
    """Play one round of the dice game. Return True if player wins, False if loses."""
    first_roll = roll_dice()
    
    if first_roll in [7, 11]: # if the sum is 7 or 11 player wins
        print("You win!")
        return True
    elif first_roll in [2, 3, 12]: # if the sum is 2, 3, or 12 player loses
        print("You lose!")
        return False
    else:
        point = first_roll
        print(f"Point is set to {point}. Keep rolling!")
        
        while True:
            roll = roll_dice()
            if roll == point:
                print("You rolled your point again! You win!")
                return True
            elif roll == 7:
                print("You rolled a 7. You lose!")
                return False

def main():
    wins = 0
    losses = 0

    while True:
        play = input("\nDo you want to play? (yes/no): ").strip().lower()
        if play == "yes":
            if play_dice_game():
                wins += 1
            else:
                losses += 1
            print(f"Current stats: {wins} Wins, {losses} Losses")
        elif play == "no":
            print(f"\nFinal stats: {wins} Wins, {losses} Losses")
            print("Thanks for playing!")
            break
        else:
            print("Please type 'yes' or 'no'.")

if __name__ == "__main__":
    print("Welcome to the Dice Game!")
    main()
Welcome to the Dice Game!
You rolled 5 + 2 = 7
You win!
Current stats: 1 Wins, 0 Losses
You rolled 5 + 2 = 7
You win!
Current stats: 2 Wins, 0 Losses

Final stats: 2 Wins, 0 Losses
Thanks for playing!