Ahaan

Random Algorithms & Simulations

  • Simulations
  • Random Algorithms

    Popcorn Hack #1: Brainstorm

    Q: What do you think a random algorithm is?

    • Answer: A random algorithm is a set of instructions that includes randomness or unpredictability to help make decisions or generate outputs.

    Q: What would be a reason to use random algorithms in real-life coding situations?

    • Answer: To make programs more efficient, simulate real-world situations (like weather or behavior), create variety (like in games), or ensure fairness (like randomized trials or voting).

    Q: What kind of questions do you think College Board will ask regarding Random Algorithms?

    • Answer: They’ll likely ask questions like how random values can affect outcomes or what range of values a RANDOM() function produces.

    Popcorn Hack 2

    # Popcorn Hack Number 2 (Random): Make a random algorithm to choose a daily activity:
    import random
    # Step 1: Define a list of activities
    activities = ['Playing Brawl Stars', 'Read a book', 'Watching Youtube', 'Play on Nintendo Switch', 'Watch a movie', 'Take a nap']
    # 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 3

    # Popcorn Hack Number 3: Using a loops in random
    # This popcorn hack assigns an activity to each person
    import random
    hosts = ['Ahaan', 'Arnav', 'Weston']
    activities = ['code, code, code', 'games', 'movie']
    # 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]}!")
    
    
    Ahaan will be monitoring movie!
    Arnav will be monitoring code, code, code!
    Weston will be monitoring games!
    

    College Board Application: Looking at MCQ’s

    img

    Answer: C This is correct because Answer C is not possible on the first iteration, i is 2, so pick can only be 1 or 2. Since 3 can’t be chosen at that point, the sequence starting with 3 can’t occur.

    MC Question

    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

    Answer: D This option is correct because it gives a 75% chance of success by covering numbers 1 through 75 out of 100 possible outcomes.

    Simulations

    Popcorn Hack 1

    import random
    
    def roll_spin():
        return random.randint(1, 68)
    
    dice_roll = roll_spin()
    print("Number:", dice_roll)
    
    Number: 54
    

    Popcorn Hack 2

    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: scissors
    You chose: rock
    You win!
    

    College Board Application: Looking at MCQ’s

    Which of the following strategies is LEAST likely to provide a more accurate prediction?

    • A: Gathering data for additional years to try to identify patterns in birth rates
    • B: Refining the model used in the computer simulation to more closely reflect the data from the past ten years
    • C: Removing as many details from the model as possible so that calculations can be performed quickly
    • D: Taking into consideration more information about the community, such as the ages of residents

    Answer: C Answer C is correct because removing details from a model may speed up calculations, but it reduces accuracy. Accurate predictions require more relevant data, not less.

    Code for the simulation is shown below.

    days ← 0 numMice ← InitialMousePopulation() numPredators ← InitialPredatorPopulation() REPEAT UNTIL (days = 365) { numMice ← NextDayPopulation(numMice, numPredators) days ← days + 1 } DISPLAY(“There are”) DISPLAY(numMice) DISPLAY(“mice after one year.”)

    Based on the code, which of the following assumptions is made in the simulation?

    • A: The number of mice increases by 1 each day.
    • B: The number of mice does not change from day to day.
    • C: The number of predators increases by 1 each day.
    • D: The number of predators does not change from day to day.

    Answer: D This is correct because the simulation sets the number of predators at the start but never changes it throughout the program. This means the model assumes the predator population stays constant each day.

    Random Algorithms

    Homework Hack 1

    import random
    
    # List of 15 students
    students = [
        'Alice', 'Ahaan', 'Arnav', 'Weston', 'Ameya', 
        'Manas', 'Grace', 'Lanzi', 'Brown', 'Jade', 
        'Kyle', 'Luna', 'Travis', 'James', 'Oscar'
    ]
    
    # Define 3 creative team names
    teams = {
        'Team Del Norte': [],
        'Team Westview': [],
        'Team Poway High': []
    }
    
    # Randomly assign students to teams
    for student in students:
        chosen_team = random.choice(list(teams.keys()))
        teams[chosen_team].append(student)
    
    # Print team assignments
    print("Random Team Assignments:\n")
    for team_name, members in teams.items():
        print(f"{team_name}: {', '.join(members)}")
    
    
    
    
    
    Random Team Assignments:
    
    Team Del Norte: Ahaan, Arnav, Brown, Jade, Kyle, Luna, Oscar
    Team Westview: Alice, Weston, Manas, Lanzi
    Team Poway High: Ameya, Grace, Travis, James
    

    Homework Hack 2

    import random
    
    # Possible weather types
    weather_types = ['Sunny', 'Cloudy', 'Rainy']
    
    # Generate forecast for 7 days
    print("7-Day Weather Forecast:")
    for day in range(1, 8):
        forecast = random.choice(weather_types)
        print(f"Day {day}: {forecast}")
    
    
    7-Day Weather Forecast:
    Day 1: Cloudy
    Day 2: Rainy
    Day 3: Sunny
    Day 4: Rainy
    Day 5: Cloudy
    Day 6: Rainy
    Day 7: Sunny
    

    Simulations

    Homework Hack 1

    import random
    
    # 5 customers with random service times between 1 and 5 minutes
    service_times = []
    for i in range(1, 6):
        time = random.randint(1, 5)
        service_times.append(time)
        print(f"Customer {i} will take {time} minutes to be served.")
    
    # Calculate the total service time
    total_time = sum(service_times)
    print(f"\nTotal time to serve all customers: {total_time} minutes")
    
    Customer 1 will take 4 minutes to be served.
    Customer 2 will take 4 minutes to be served.
    Customer 3 will take 3 minutes to be served.
    Customer 4 will take 4 minutes to be served.
    Customer 5 will take 2 minutes to be served.
    
    Total time to serve all customers: 17 minutes
    
    Scroll to top