Popcorn Hack #1: Brainstorm
What do you think a random algorithm is?
A random algorithm is a type of algorithm that incorporates randomness or chance into its logic. This means that the algorithm might produce different outputs or follow different paths even when given the same input, depending on randomly generated values during its execution.
What would be a reason to use random algorithms in real-life coding situations?
Random algorithms are useful in a variety of real-world applications, such as:
- Simulations: Modeling complex systems like traffic, weather, or crowd behavior.
- Gaming: Creating unpredictable behaviors in games for enemies or loot drops.
- Sampling: Selecting a random subset of data, useful in surveys or big data.
- Optimization: Solving problems where trying every possibility is too slow (e.g., randomized optimization or genetic algorithms).
- Cryptography: Ensuring secure and unpredictable encryption.
They are often used when:
- A deterministic solution is too slow or complex.
- You need variety or unpredictability.
- You want to avoid bias.
What kind of questions do you think College Board will ask regarding Random Algorithms?
The College Board might ask questions such as:
- Predicting the possible outputs of a program that uses a random number generator.
- Explaining how changing the range or frequency of random values affects the behavior of an algorithm.
- Understanding the purpose and impact of randomness in a given code snippet.
- Simulating the outcomes of repeated trials or experiments using random values.
import random
# Step 1: Define a list of activities (customized)
activities = [
'Paint something cool',
'Go skateboarding',
'Learn a dance from TikTok',
'Build a LEGO set',
'Try a new video game',
'Bake cookies',
'Do a mini workout',
'Organize your room',
'Start a new show',
'Take aesthetic photos outside'
]
# 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: Try a new video game
# Popcorn Hack Number 3: Using loops with random
# This popcorn hack assigns an activity to each host at the party
import random
# Step 1: Define your list of party hosts and activities
hosts = ['Liam', 'Zara', 'Noah', 'Ava', 'Maya']
activities = ['drink station', 'music booth', 'photo wall', 'snack table', 'game zone']
# Step 2: Randomly shuffle the activities
random.shuffle(activities)
# Step 3: Loop through and assign each host to an activity
for i in range(len(hosts)):
print(f"{hosts[i]} will be monitoring the {activities[i]}!")
Liam will be monitoring the game zone!
Zara will be monitoring the snack table!
Noah will be monitoring the drink station!
Ava will be monitoring the photo wall!
Maya will be monitoring the music booth!
Simulation Games Popcorn Hack 1
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): ").lower()
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: scissors
You chose: rock
You win!
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): ").lower()
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!")
# Run the game
play_rock_paper_scissors()
Computer chose: paper
You chose: rock
You lose!
Homework Hacks- completed all 3
import random
# List of 15 students
students = [
"Alex", "Bria", "Carlos", "Dina", "Eli", "Farah", "Gabe", "Hana", "Isaac", "Jade",
"Kian", "Lena", "Milo", "Nina", "Omar"
]
# Creative team names
teams = ["Team Phoenix", "Team Thunder", "Team Eclipse"]
# Create a dictionary to hold team assignments
team_assignments = {team: [] for team in teams}
# Shuffle the student list to randomize order
random.shuffle(students)
# Assign each student to a team in round-robin fashion
for i, student in enumerate(students):
team = teams[i % len(teams)]
team_assignments[team].append(student)
# Print out the results
for team, members in team_assignments.items():
print(f"\n{team} Members:")
for member in members:
print(f" - {member}")
Team Phoenix Members:
- Eli
- Carlos
- Bria
- Alex
- Omar
Team Thunder Members:
- Milo
- Farah
- Lena
- Jade
- Kian
Team Eclipse Members:
- Dina
- Gabe
- Hana
- Nina
- Isaac
import random
# List of possible weather types
weather_types = ['Sunny', 'Cloudy', 'Rainy']
# Number of days in the forecast
days = 7
# Generate and print the weather for each day
print("🌤️ 7-Day Weather Forecast 🌧️\n")
for day in range(1, days + 1):
weather = random.choice(weather_types)
print(f"Day {day}: {weather}")
🌤️ 7-Day Weather Forecast 🌧️
Day 1: Sunny
Day 2: Sunny
Day 3: Sunny
Day 4: Cloudy
Day 5: Cloudy
Day 6: Sunny
Day 7: Rainy
import random
# Simulate 5 customers
customers = ["Customer 1", "Customer 2", "Customer 3", "Customer 4", "Customer 5"]
# Store service times
service_times = []
# Generate random service times between 1 and 5 minutes for each customer
for customer in customers:
service_time = random.randint(1, 5)
service_times.append(service_time)
print(f"{customer} will take {service_time} minute(s) to be served.")
# Calculate total time
total_time = sum(service_times)
print(f"\nTotal time to serve all customers: {total_time} minutes")
Customer 1 will take 4 minute(s) to be served.
Customer 2 will take 2 minute(s) to be served.
Customer 3 will take 4 minute(s) to be served.
Customer 4 will take 2 minute(s) to be served.
Customer 5 will take 3 minute(s) to be served.
Total time to serve all customers: 15 minutes