Big Idea 5.8 Simulations
Software Development using Frontend and Backend Technologies
Popcorn Hack 1
Random numbers are used to decide things like dice rolls, card shuffles, or help create secure passwords and encryption keys, making it harder for hackers to guess or break into systems.
Popcorn Hack 2
import random
def magic_8_ball():
# Generate a random number between 1 and 100
num = random.randint(1, 100)
if num <= 50:
return "Yes"
elif num <= 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: Ask again later
Magic 8-Ball says: No
Magic 8-Ball says: Ask again later
Magic 8-Ball says: Ask again later
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
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
Answer to question
This is a simulation because it model how a real traffic light changes over time without actually using a real traffic light. Its real-world impact is helping people test/understand traffic patterns, and can make roads safer.
Homework Hack 1
import random
def roll_dice():
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
total = die1 + die2
print(f"You rolled {die1} + {die2} = {total}")
return die1, die2, total
def play_dice_game():
_, _, total = roll_dice()
# First roll outcomes
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}. Keep rolling!")
# Keep rolling until point or 7
while True:
_, _, total = roll_dice()
if total == point:
print("You rolled the 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("\nDo you want to play a round? (yes/no): ").strip().lower()
if play in ['yes', 'y']:
if play_dice_game():
wins += 1
else:
losses += 1
print(f"Current Stats - Wins: {wins}, Losses: {losses}")
elif play in ['no', 'n']:
print("\nThanks for playing!")
print(f"Final Stats - Wins: {wins}, Losses: {losses}")
break
if __name__ == "__main__":
main()