Popcorn Hack 1: Real-World Applications
Question: Name two real-world applications where random number generation is essential and briefly explain why.
Answer:
- Cybersecurity – Random number generators are used to create encryption keys which are essential to secure online communication.
- Game Development – Random numbers are used for things like loot drops, enemy behavior, or card shuffling to ensure unpredictability and fairness.
🎱 Popcorn Hack 2: Magic 8-Ball
import random
def magic_8_ball():
roll = random.randint(1, 4)
if roll <= 2:
return "Yes"
elif roll == 3:
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: Ask again later
Magic 8-Ball says: Ask again later
Magic 8-Ball says: Yes
Magic 8-Ball says: No
Magic 8-Ball says: Ask again later
Magic 8-Ball says: Yes
Magic 8-Ball says: No
Magic 8-Ball says: No
Popcorn Hack 3
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
Time 20: Red
This is a simulation because it models the behavior of a traffic light system over time using predefined rules. Its real-world impact includes helping city planners design efficient traffic flow systems and improving road safety.
HW Hack 1
import random
def roll_dice():
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
total = die1 + die2
return die1, die2, total
def play_dice_game():
die1, die2, total = roll_dice()
print(f"Player 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"Point is set to {point}")
while True:
die1, die2, total = roll_dice()
print(f"Rolling again: {die1} + {die2} = {total}")
if total == point:
print("You win!")
return True
elif total == 7:
print("You lose!")
return False
def main():
wins = 0
losses = 0
while True:
play = input("Do you want to play a round? (yes/no): ").strip().lower()
if play == "yes":
if play_dice_game():
wins += 1
else:
losses += 1
elif play == "no":
print(f"Final Stats - Wins: {wins}, Losses: {losses}")
break
else:
print("Please enter 'yes' or 'no'.")
if __name__ == "__main__":
print("Welcome to the Dice Game!")
main()
Welcome to the Dice Game!
Player rolled: 2 + 2 = 4
Point is set to 4
Rolling again: 4 + 1 = 5
Rolling again: 1 + 6 = 7
You lose!
Player rolled: 5 + 2 = 7
You win!
Final Stats - Wins: 1, Losses: 1