Random algorithms are a random way to pick a value form a set. Cryptography is a good exmple of random algorithms as well as games and stocks.
# Popcorn Hack Number 2 (Random): Make a random algorithm to choose a daily activity:
import random
# Step 1: Define a list of activities
activities = ['Game', 'eat', 'sleep', 'study', 'Watch a movie', 'run', 'facetime a friend']
# Step 2: Randomly choose an activity
random_activity = random.choice(activities)
# Step 3: Display the chosen activity
print(f"Today’s random activity: {random_activity}")
Today’s random activity: Watch a movie
# Popcorn Hack Number 3: Using a loops in random
# This popcorn hack assigns an activity to each person
import random
hosts = ['Gavin Copley', 'Elliot Bang', 'Johan']
activities = ['take a selfie', 'games', 'sleep', 'party', 'dance']
# Randomly shuffle the list of activities to assign them randomly to the guests
random.shuffle(activities)
# Loop through each guest and assign them a random activity
for i in range(len(hosts)):
print(f"{hosts[i]} will be monitoring {activities[i]}!")
import random
def roll_dice():
return random.randint(1, 690000000000)
dice_roll = roll_dice()
print("Number:", dice_roll)
Number: 657948189499
import random
def play_rock_paper_scissors():
choices = ['rock', 'paper', 'scissors']
computer_choice = random.choice(choices)
user_choice = input("Enter your choice (rock, paper, or scissors): ")
if user_choice not in choices:
print("Invalid choice. Please try again.")
return
print("Computer chose:", computer_choice)
print("You chose:", user_choice)
if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == 'rock' and computer_choice == 'scissors') or (user_choice == 'paper' and computer_choice == 'rock') or (user_choice == 'scissors' and computer_choice == 'paper'):
print("You win!")
else:
print("You lose!")
play_rock_paper_scissors()
Computer chose: rock
You chose: rock
It's a tie!
Homeworks:
import random
students = [
"Alice", "Bob", "Charlie", "Diana", "Ethan",
"Fiona", "George", "Hannah", "Ivan", "Jade",
"Kyle", "Liam", "Maya", "Nina", "Oscar"
]
teams = {
"Team Thunder": [],
"Team Lightning": [],
"Team Tornado": []
}
for student in students:
team_name = random.choice(list(teams.keys()))
teams[team_name].append(student)
print("Team Assignments:\n")
for team, members in teams.items():
print(f"{team}: {', '.join(members)}")
=============================================
weather_types = ["Sunny", "Cloudy", "Rainy"]
forecast = []
for day in range(1, 8):
weather = random.choice(weather_types)
forecast.append(weather)
print(f"Day {day}: {weather}")
============================================
service_times = [random.randint(1, 5) for _ in range(5)]
total_time = 0
print("\nCoffee Shop Queue Simulation:\n")
for i, time in enumerate(service_times):
total_time += time
print(f"Customer {i+1} service time: {time} minutes")
print(f"\nTotal time to serve all customers: {total_time} minutes")