Home | Random Algorithms | Simulation Games | Homework |
Random Algorithms
Abby, Risha, Soumini, Ethan
Random Algorithms
College Board Big Ideas
- 3.9 Developing Algorithms
- 3.15 Random Values
Popcorn Hack #1: Brainstorm
What do you think a random algorithm is? What would be reason to use random algorithms in real-life coding situations? What kind of questions do you think College Board will ask regarding Random Algorithms?
Defining Random Algorithms
Random algorithms are used to pick a random value from a certain set. We can use random algorithms to make simulations that model the real world.
Real Life Application
Random algorithms, also known as randomized algorithms, are used in various real-life applications where randomness can improve efficiency, ensure fairness, or handle uncertainty. Here are some key areas where they are applied:
1. Cryptography & Cybersecurity
- Secure encryption (e.g., RSA, AES) relies on random key generation.
- Hashing algorithms use randomness to secure passwords.
- Secure communication protocols like SSL/TLS use random numbers for authentication.
2. Machine Learning & AI
- Random forest algorithms for classification and decision-making.
- Stochastic gradient descent (SGD) for training deep learning models.
- Data sampling and augmentation for model generalization.
3. Computational Biology & Medicine
- Genetic algorithms simulate evolution to find optimal solutions.
- Monte Carlo simulations model biological processes.
- Randomized clinical trials ensure unbiased medical research.
4. Finance & Stock Market
- Monte Carlo simulations for risk assessment and option pricing.
- Algorithmic trading strategies use randomization to prevent exploitation.
- Portfolio diversification strategies use randomness to reduce risk.
5. Networking & Load Balancing
- Randomized routing algorithms distribute network traffic efficiently.
- Load balancers use random selection to distribute requests among servers.
- Backoff algorithms in TCP/IP networking use randomness to avoid collisions.
6. Gaming & Simulations
- Procedural generation in video games (e.g., terrain, maps, AI behaviors).
- Randomized AI behaviors make games more unpredictable.
- Monte Carlo methods are used in physics-based simulations.
7. Optimization Problems
- Simulated annealing for finding near-optimal solutions.
- Randomized search algorithms in large datasets.
- Genetic algorithms for engineering design and problem-solving.
8. Fairness & Decision-Making
- Randomized voting methods reduce bias in elections.
- Randomized controlled trials (RCTs) in social science research.
- Lottery systems for resource allocation (e.g., visas, schools).m
Random Python Module
Random is also a python function. It can be used by importing the random module.
List generator example
Mr Mort’s joke randomizer example
This first example comes from the Github Pages Playground project in Trimester 1. Mr Mort uses random to generate jokes from te compsci_joke_list. Use the following steps to see the printed message.
- In VSCode, go to Help->Toggle Developer Tools
- Click on Console in the newly opened Window
- Clear screen by pressing Clear Console (circle with line)
- Then press Play button to left of cell
- Observe random output in Console
%%javascript
var compsci_joke_list = [
{ joke: "Why do programmers prefer dark mode? Because light attracts bugs.", complexity: "O(1)" },
{ joke: "Why do Java developers wear glasses? Because they don't see sharp.", complexity: "O(1)" },
{ joke: "How many programmers does it take to change a light bulb? None, that's a hardware problem.", complexity: "O(1)" },
{ joke: "Why do programmers hate nature? It has too many bugs.", complexity: "O(n)" },
{ joke: "Why do Python programmers prefer snake_case? Because they can't C.", complexity: "O(1)" },
{ joke: "Why was the JavaScript developer sad? Because he didn't know how to 'null' his feelings.", complexity: "O(1)" },
{ joke: "Why do programmers always mix up Christmas and Halloween? Because Oct 31 == Dec 25.", complexity: "O(1)" },
{ joke: "Why did the programmer quit his job? Because he didn't get arrays.", complexity: "O(n)" },
{ joke: "Why do programmers prefer using the terminal? Because they don't like Windows.", complexity: "O(1)" },
{ joke: "Why was the function sad after a breakup? It couldn't find its closure.", complexity: "O(1)" }
];
var randomIndex = Math.floor(Math.random() * compsci_joke_list.length);
var selectedJoke = compsci_joke_list[randomIndex];
console.log("Joke #" + (randomIndex + 1) + ": " + selectedJoke.joke + " (Complexity: " + selectedJoke.complexity + ")");
<IPython.core.display.Javascript object>
Popcorn Hack #2
Copy and Paste this code into a Python cell in your jupyter notebook, change the types of activities, and see which activity you created gets chosen. Once everyone is done, share your results!
# Popcorn Hack Number 2 (Random): Make a random algorithm to choose a daily activity:
import random
# Step 1: Define a list of activities
activities = ['Go for a walk', 'Read a book', 'Call a friend', 'Try a new recipe', 'Watch a movie', 'Take a nap', 'Go for a run', 'Write in a journal', 'Meditate', 'Listen to music', 'Be productive and do your homework']
# 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}")
Popcorn Hack #3
You and your team are hosting a party. With your team, make a random algorithm to help them decide which person should monitor which activity. Copy and Paste this code into a Python cell in your jupyter notebook, change the names and types of activities, and see which gets chosen.
# Popcorn Hack Number 3: Using a loops in random
# This popcorn hack assigns an activity to each person
import random
hosts = ['Abby', 'Ethan', 'Risha', 'Soumini', 'Shriya']
activities = ['dancing', 'games', 'snack center', 'photo booth', 'karaoke']
# 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]}!")
College Board Application: Looking at MCQ’s
Let’s look at some previous questions from College Board regarding Big Idea 3.15 Random Values.
Analyzing Code Segments
Science experiment with 75 percent successful trials
In a certain science experiment, 75 percent of trials are expected to be successful and 25 percent of trials are expected to be unsuccessful. The program below is intended to simulate the results of repeated trials of the experiment.
successful ← 0
unsuccessful ← 0
REPEAT 1000 TIMES
{
IF (<MISSING CODE>)
{
successful ← successful + 1
}
ELSE
{
unsuccessful ← unsuccessful + 1
}
}
DISPLAY(successful)
DISPLAY("trials were successful,")
DISPLAY(unsuccessful)
DISPLAY("trials were unsuccessful.")
Which of the following can be used to replace < MISSING CODE > so that the simulation works as intended?
- A: RANDOM, open parenthesis 1 comma 100, close parenthesis, equals 25
- B: RANDOM, open parenthesis 1 comma 100, close parenthesis, is less than or equal to 25
- C: RANDOM, open parenthesis 1 comma 100, close parenthesis, equals 75
- D: RANDOM, open parenthesis 1 comma 100, close parenthesis, is less than or equal to 75