Online Security (Cryptography) – Random numbers are essential for generating secure encryption keys, which protect data during transmission. Without true randomness, hackers could predict keys and access sensitive information.
Video Games – Random number generation is used to create unpredictable gameplay elements like loot drops or enemy behavior, making games more engaging and replayable.
import random
def magic_8_ball():
# TODO: Generate a random number and use it to return one of
# the three possible answers with the correct probabilities
# Your code here
num=random.randint(1,4)
if num <=2:
return "yes"
elif num==3:
return "no"
elif num==4:
return "ask again later"
# Test your function
results = {}
for i in range(200):
result = magic_8_ball()
results[result] = results.get(result, 0) + 1
print("Results of 20 spins:")
for color, count in results.items():
print(f"{color}: {count} times ({count/200*100:.1f}%)")
Results of 20 spins:
yes: 99 times (49.5%)
no: 49 times (24.5%)
ask again later: 52 times (26.0%)
# Traffic light simulation (no randomness)
states = ["Green", "Yellow", "Red"]
durations = {"Green": 5, "Yellow": 2, "Red": 4}
timeline = []
# Simulate 10 time steps
time = 0
state = "Green"
counter = 0
while time < 10:
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
This is a simulation because it models traffic light behavior using generated data instead of real-world input. It cycles through light states based on preset durations, allowing us to observe and test timing patterns. The real-world impact of such simulations includes improving traffic efficiency, reducing wait times, and designing safer intersections before implementing changes in actual traffic systems.
import random
def roll_dice():
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():
first_roll = roll_dice()
if first_roll in [7, 11]:
print("You win!")
return True
elif first_roll in [2, 3, 12]:
print("Craps! You lose!")
return False
else:
point = first_roll
print(f"Your point is {point}. Keep rolling until you get {point} to win or 7 to lose.")
while True:
roll = roll_dice()
if roll == point:
print("You hit your point! You win!")
return True
elif roll == 7:
print("You rolled a 7. You lose!")
return False
def main():
wins = 0
losses = 0
while True:
choice = input("Do you want to play a round? (yes/no): ").strip().lower()
if choice == "yes":
result = play_dice_game()
if result:
wins += 1
else:
losses += 1
print(f"Current Stats - Wins: {wins}, Losses: {losses}\n")
elif choice == "no":
print(f"Final 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!
Rolled: 6 + 5 = 11
You win!
Current Stats - Wins: 1, Losses: 0
Rolled: 6 + 2 = 8
Your point is 8. Keep rolling until you get 8 to win or 7 to lose.
Rolled: 2 + 5 = 7
You rolled a 7. You lose!
Current Stats - Wins: 1, Losses: 1
Rolled: 3 + 2 = 5
Your point is 5. Keep rolling until you get 5 to win or 7 to lose.
Rolled: 3 + 2 = 5
You hit your point! You win!
Current Stats - Wins: 2, Losses: 1
Rolled: 2 + 3 = 5
Your point is 5. Keep rolling until you get 5 to win or 7 to lose.
Rolled: 3 + 4 = 7
You rolled a 7. You lose!
Current Stats - Wins: 2, Losses: 2
Rolled: 3 + 1 = 4
Your point is 4. Keep rolling until you get 4 to win or 7 to lose.
Rolled: 5 + 6 = 11
Rolled: 6 + 2 = 8
Rolled: 1 + 2 = 3
Rolled: 6 + 3 = 9
Rolled: 1 + 1 = 2
Rolled: 2 + 5 = 7
You rolled a 7. You lose!
Current Stats - Wins: 2, Losses: 3
Rolled: 1 + 2 = 3
Craps! You lose!
Current Stats - Wins: 2, Losses: 4
Rolled: 4 + 3 = 7
You win!
Current Stats - Wins: 3, Losses: 4
Rolled: 5 + 3 = 8
Your point is 8. Keep rolling until you get 8 to win or 7 to lose.
Rolled: 4 + 4 = 8
You hit your point! You win!
Current Stats - Wins: 4, Losses: 4
Rolled: 2 + 6 = 8
Your point is 8. Keep rolling until you get 8 to win or 7 to lose.
Rolled: 6 + 3 = 9
Rolled: 4 + 6 = 10
Rolled: 5 + 5 = 10
Rolled: 3 + 5 = 8
You hit your point! You win!
Current Stats - Wins: 5, Losses: 4
Rolled: 4 + 2 = 6
Your point is 6. Keep rolling until you get 6 to win or 7 to lose.
Rolled: 2 + 4 = 6
You hit your point! You win!
Current Stats - Wins: 6, Losses: 4
Rolled: 2 + 3 = 5
Your point is 5. Keep rolling until you get 5 to win or 7 to lose.
Rolled: 3 + 3 = 6
Rolled: 3 + 3 = 6
Rolled: 6 + 5 = 11
Rolled: 3 + 4 = 7
You rolled a 7. You lose!
Current Stats - Wins: 6, Losses: 5
Final Stats - Wins: 6, Losses: 5
Thanks for playing!