Objectives

  1. Understand how computers can be used to represent real-world phenomena or outcomes
  2. Compare simulations with real-world contexts.
  3. Implement code to mimic real world situations, problems, or phenomena.

What are simulations by College Board definition?

  • Simulations are models that mimic more complex objects or phenomena from the real world
    • Purposes include drawing inferences without the costs or risks of the real world
  • Simulations use varying sets of values to reflect the current state of a real phenomenon
  • Often, when developing a simulation, it is necessary to remove specific variables or simplify aspects
    • Simulations can often contain biases based on which details or real-world elements were included/excluded
  • Simulations allow the formulation of properties under consideration
  • Variability and randomness of the world is considered using random number generators
  • Examples: rolling dice, spinners, molecular models, analyze chemicals/reactions...

Analyzing an Example: Air-Traffic Simulator

  • Say we want to find out what the optimal number of aircrafts that can be in the air in one area is.
  • A simulation allows us to explore this question without real world contraints of money, time, safety
    • Unfortunately we can't just fly 67 planes all at once and see what happens
  • Since the simulation won't be able to take all variables into control, it may have a bias towards one answer
  • Will not always have the same result

Functions we often need (python)

import random # a module that defines a series of functions for generating or manipulating random integers
random.choice() #returns a randomly selected element from the specified sequence
random.choice(mylist) # returns random value from list
random.randint(0,10) #randomly selects an integer from given range; range in this case is from 0 to 10
random.random() #will generate a random float between 0.0 to 1.

Functions we often need (js)

// Math.random(); returns a random number
// Math.floor(Math.random() * 10); // Returns a random integer from 0 to 9:

College Board Question 1

Question: The following code simulates the feeding of 4 fish in an aquarium while the owner is on a 5-day trip:

numFish ← 4

foodPerDay ← 20

foodLeft ← 160

daysStarving ← 0

    REPEAT 5 TIMES {

    foodConsumed ← numFish * foodPerDay

    foodLeft ← foodLeft - foodConsumed

    IF (foodLeft < 0) {

    daysStarving ← daysStarving + 1

    }

}

  • This simulation simplifies a real-world scenario into something that can be modeled in code and executed on a computer.
  • Summarize how the code works: The code runs five times after the initial variables have been set and each time, the number

Examples

Card Flip

import random

cards = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] 
suits = ["Diamonds", "Hearts", "Spades", "Clubs"]

print(random.choice(cards) + " of " + random.choice(suits))

Coin Flip

import random

def coinflip():         #def function 
    randomflip = random.randint(0, 1) #picks either 0 or 1 randomly 
    if randomflip == 0 : #assigning 0 to be heads--> if 0 is chosen then it will print, "Heads"
        print("Heads")
    else:
        if randomflip == 1: #assigning 1 to be tails--> if 1 is chosen then it will print, "Tails"
            print("Tails")

#Tossing the coin 5 times:
t1 = coinflip()
t2 = coinflip()
t3 = coinflip()
t4 = coinflip()
t5 = coinflip()
Heads
Heads
Tails
Heads
Tails

Your turn: Change the code to make it simulate the flipping of a weighted coin.

import random

def coinflip():         #def function 
    randomflip = random.randint(0,2) #picks either 0 or 1 or 2 randomly 
    if randomflip == 0 or randomflip == 2: #assigning 0 or 2 to be heads--> if 0 or 2 is chosen then it will print, "Heads"
        print("Heads")
    else:
        if randomflip == 1: #assigning 1 to be tails--> if 1 is chosen then it will print, "Tails"
            print("Tails")

#Tossing the coin 5 times:
t1 = coinflip()
t2 = coinflip()
t3 = coinflip()
t4 = coinflip()
t5 = coinflip()
Heads
Heads
Heads
Tails
Heads

Adding images (in Python)

  • Add a heads and tails images into your images directory with the correct names and run the code below
import random

# importing Image class from PIL package
from PIL import Image
 
# creating a object
im = Image.open(r"images/HeadsOn2.png")
image = Image.open(r"images/TailsOn2.png")

i=random.randint(0,1)

if i == 1:
    print("heads")
    display(im)

else:
    print("tails")
    display(image)
heads

In order to display an image in python, we can use the PIL package we previously learned about.

Spin the Wheel

import random

print("Spin the wheel!")
print("----------------------------------")

n = 300
blue = 0
red = 0
 
for i in range(n):
    spin = random.randint(1,2)
    if spin == 1: # head
        blue = blue + 1
    else:         # tail
        red = red + 1
 
print('Number of blue:', blue)
print('Number of red:', red)
Spin the wheel!
----------------------------------
Number of blue: 145
Number of red: 155

Your turn: Add a visual to the simulation!

import random
import matplotlib.pyplot as plt

print("Spin the wheel!")
print("----------------------------------")

n = 300
blue = 0
red = 0
 
for i in range(n):
    spin = random.randint(1,2)
    if spin == 1: # head
        blue = blue + 1
    else:         # tail
        red = red + 1
 
print('Number of blue:', blue)
print('Number of red:', red)

# Plot the results in a bar graph
colors = ['Blue', 'Red']
counts = [blue, red]
plt.bar(colors, counts, color=['blue', 'red'])
plt.title('Spinning Wheel: Count vs. Color')
plt.xlabel('Color')
plt.ylabel('Count')
plt.show()
Spin the wheel!
----------------------------------
Number of blue: 139
Number of red: 161

Population Growth and Plots

import random

totalPopulation = 50 
growthFactor = 1.00005
dayCount = 0 #Every 2 months the population is reported

while totalPopulation < 1000000:
    totalPopulation *= growthFactor
    #Every 56th day, population is reported
    dayCount += 1
    if dayCount == 56: 
        dayCount = 0
        print(totalPopulation)

code works but didn't want to run as output is really long

Here we initialize the total population to be 50, then set the growth factor as 1.00005 (.005 percent change). It will print the population every 56th day until it reaches one million. It multiplies the current population by the growth factor in each iteration, and increments the day count. When the day count reaches 56, it prints the current population and resets the day count to 0.

Note! This simulation assumes that the growth factor remains constant as time progresses, which may not be a realistic assumption in real-world scenarios.

import matplotlib.pyplot as plt

# Define the initial population and growth rate
population = 100
growth_rate = 0.05

# Define the number of years to simulate
num_years = 50

# Create lists to store the population and year values
populations = [population]
years = [0]

# Simulate population growth for the specified number of years
for year in range(1, num_years+1):
    # Calculate the new population size
    new_population = population + (growth_rate * population)
    # Update the population and year lists
    populations.append(new_population)
    years.append(year)
    # Set the new population as the current population for the next iteration
    population = new_population
    
# Plot the population growth over time
plt.plot(years, populations)
plt.xlabel('Year')
plt.ylabel('Population')
plt.title('Population Growth Simulation')
plt.show()

If we create quantative data, we can plot it using the Matplotlib library.

Example on how simplification can cause bias

import random

beak =  ["small-beak", "long-beak", "medium-beak"],
wing = ["small-wings", "large-wings", "medium-wings"],
height = ["short", "tall","medium"]


naturaldisaster = ["flood", "drought", "fire", "hurricane", "dustbowl"]


print("When a" , random.choice(naturaldisaster) , "hit",  random.choice(height), "birds died") 
When a drought hit medium birds died

How does this simulation have bias?

This simulation happens to have bias since there are no factors that determine when a certain group of birds died and it isn't properly stimulating the situation + the code is only randomizing the height of the bird rather than all the other factors that may also affect he likely hood of the bird living a certain natural disaster.

JS examples

Hacks

  • Answer all questions and prompts in the notes (0.2)
  • Create a simulation
    1. Create a simulation that uses iteration and some form of data collection (list, dictionary...) (0.4)
      • try creating quantative data and using the Matplotlib library to display said data
      • Comment and describe function of each parts
      • How does your simulation help solve/mimic a real world problem?
      • Is there any bias in your simulation? Meaning, are there any discrepancies between your program and the real event?
  • Answer these simulation questions (0.3)
  • Bonus: take a real world event and make a pseudocode representation or pseudocode on a flowchart of how you would make a simulation for it (up to +0.1 bonus)

Due Thursday Night (11:59)

Card Hands Simulation

  • The purpose of this simulation is to create two different hands of cards after randomly assigning the hands the cards and adding up that score and comparing them via a graph made from pyplot
  • The simulation mimics the real world situation of dealing cards and scoring them, it can be used for a variety of things such as deciding over something or just to simply have fun. It simulates the deck through creating a card in which each value is assigned each symbol then by using random to shuffle the deck and then using a procedure to score the cards and a for loop to go through each card and assign them a score.
  • There is a slight bit of bias but that is because some people may consider the values to be different, causing my simulation to be accurate unless the values for each of the cards are assigned something else other wise, this simulation is pretty accurate to the real world situation and I think bias was avoided pretty well
import random
import matplotlib.pyplot as plt

card_values = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
card_symbol = ["Hearts", "Diamonds", "Clubs", "Spades"]
# Defines te values and symbols on the cards


deck = [(value, symbol) for value in card_values for symbol in card_symbol]
# creates a deck of cards

random.shuffle(deck)
# shuffles the deck

print("Hand 1:\n")
score = 0
# creating the first hand of cards 

def score_card(card1):
    if card1[0] in ["Ace", "King", "Queen", "Jack"]:
        return 10
    else:
        return int(card1[0])
# function to score

for i in range(5):
    card1 = deck.pop()
    print("Your card\n", card1)
    score += score_card(card1)
# runs through each of the cards in the deck to score and sums up the score

print("\nTotal Score of Hand 1:", score)

random.shuffle(deck)

print("\n\n")
print("Hand 2:\n")
score1 = 0
# creating the second hand of cards 

def score_card(card2):
    if card2[0] in ["Ace", "King", "Queen", "Jack"]:
        return 10
    else:
        return int(card2[0])
# function to score

for i in range(5):
    card2 = deck.pop()
    print("Your card\n", card2)
    score1 += score_card(card2)
# runs through each of the cards in the deck to score and sums up the score

print("\nTotal Score of Hand 2:", score1)

print("\nScores are calculated by Picture Cards (ex. King and Ace) are 10 points and number cards are given the value of the number on the card")    

hands = ['Hand 1', 'Hand 2']
scores = [score, score1]
plt.bar(hands, scores, color=['blue', 'red'])
plt.title('Random 5 Cards from a Deck vs. Score of Cards Combined')
plt.xlabel('Hand')
plt.ylabel('Score')
plt.show()
# creating the bar graph
Hand 1:

Your card
 ('Ace', 'Hearts')
Your card
 ('King', 'Clubs')
Your card
 ('3', 'Hearts')
Your card
 ('4', 'Clubs')
Your card
 ('4', 'Diamonds')

Total Score of Hand 1: 31



Hand 2:

Your card
 ('8', 'Spades')
Your card
 ('King', 'Spades')
Your card
 ('9', 'Diamonds')
Your card
 ('5', 'Diamonds')
Your card
 ('Ace', 'Spades')

Total Score of Hand 2: 42

Scores are calculated by Picture Cards (ex. King and Ace) are 10 points and number cards are given the value of the number on the card

Collegeboard Questions

  1. A. Ride preference—denotes whether a patron prefers roller coasters, other thrill rides, gentle rides, or no rides and B. Walking preference—denotes how far a patron is willing to walk in between rides

  2. A. A representation of grass that rabbits must eat frequently to survive

  3. A. The approximate length of time until the hole would be refilled (due to various atmospheric processes)

  4. D. All of the above
  5. B. It would allow the user to see the effect of specific variables by ensuring that the others do not change and C. It would quickly provide the user with a large amount of data
  6. C. Computer simulations usually make some simplifying assumptions about the real-world object or system being modeled

Pseudocode Simulation

  • This simulation stimulates the race between the tortoise and the hare:\

tortoisespeed <-- 2 harespeed <-- 4 finishline <-- 350 tortposition <-- 0 hareposition <-- 0 lastnap <-- 0 napstartlength <-- 0 minsbetweennaps <-- 30 naplength <-- 40 while tortposition < finishline AND hareposition < finishline: tortposition = tortposition + tortoisespeed print("Tortoise position:{}".format(tortposition)) if lastnap <= minsbetweennaps: hareposition = hareposition + harespeed lastnap = lastnap + 1 print("Hare position:{}".format(hareposition)) else: if lastnap < naplength: napstartlength = napstartlength + 1 print("Hare position:{}".format(hareposition)) else: lastnap = 0 napstartlength = 0 print("Hare position:{}".format(hareposition))