Random Algorithms and Simulations/Games
Random Algorithms HW and Popcorn Hacks
Popcorn Hack 2:
import random
def magic_8_ball():
spin = random.randint(1, 4)
if spin <= 2:
return "Yes"
elif spin == 3:
return "No"
else:
return "Ask again later"
# Test
for i in range(10):
print(f"Magic 8-Ball says: {magic_8_ball()}")
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: Ask again later
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: No
Magic 8-Ball says: Ask again later
Popcorn Hack 3:
states = ["Green", "Yellow", "Red"]
durations = {"Green": 5, "Yellow": 2, "Red": 4}
timeline = []
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
-
This is a simulation because it models how a traffic light behaves over time in a system using fixed logic
-
Its real-world impact could include helping city planners design traffic flow to improve the safety of its citizens
Homework Hack 1:
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
print(f"Rolled: {die1} + {die2} = {total}")
return total
def play_dice_game():
"""
Play one round of the dice game.
Returns True if player wins, False if player loses.
"""
point = roll_dice()
if point in [7, 11]:
print("You win!")
return True
elif point in [2, 3, 12]:
print("You lose!")
return False
else:
print(f"Point is set to: {point}")
while True:
roll = roll_dice()
if roll == point:
print("You rolled the point! You win!")
return True
elif roll == 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:
choice = input("Play a round? (y/n): ").lower()
if choice == 'y':
result = play_dice_game()
if result:
wins += 1
else:
losses += 1
print(f"Stats → Wins: {wins}, Losses: {losses}")
elif choice == 'n':
print(f"Final Stats → Wins: {wins}, Losses: {losses}")
break
else:
print("Please enter 'y' or 'n'.")
if __name__ == "__main__":
print("Welcome to the Dice Game!")
main()
Welcome to the Dice Game!
Rolled: 6 + 2 = 8
Point is set to: 8
Rolled: 3 + 5 = 8
You rolled the point! You win!
Stats → Wins: 1, Losses: 0
Rolled: 3 + 3 = 6
Point is set to: 6
Rolled: 4 + 5 = 9
Rolled: 3 + 2 = 5
Rolled: 3 + 4 = 7
You rolled a 7! You lose!
Stats → Wins: 1, Losses: 1
Rolled: 2 + 5 = 7
You win!
Stats → Wins: 2, Losses: 1
Rolled: 2 + 3 = 5
Point is set to: 5
Rolled: 5 + 1 = 6
Rolled: 1 + 2 = 3
Rolled: 4 + 5 = 9
Rolled: 3 + 6 = 9
Rolled: 6 + 1 = 7
You rolled a 7! You lose!
Stats → Wins: 2, Losses: 2
Final Stats → Wins: 2, Losses: 2