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?

Take notes on vocabulary terms and concepts!

  • Simulations are abstractions that mimic more complex objects or phenomena from the real world
    • Purposes include drawing inferences without the contraints of the real world
  • Simulations use varying sets of values to reflect the changing state of a real phenomenon
  • Often, when developing a simulation, it is necessary to remove specific details or simplify aspects
    • Simulations can often contain bias based on which details or real-world elements were included/excluded
  • Simulations allow the formulation of hypotheses 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

Funtions/programs needed often when creating simulations

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.

Try using these functions!

  1. trying out functions
    • Randomly select select a number from 1-100, assign it to be x, and print it
      • if you have time, add a user input box !
  2. Use the template and create a function that will choose one thing from a list of clothes to throw out
    • Bonus: Add user input. User can deciede whether they want to "add" clothes to their closet, or "trash" something from their closet. If they chose "add," then from a new list of clothes, randomly select one piece to append to myclothes. If they chose "trash" select one item from "myclothes" to throw out.
 
def mycloset():
    myclothes = ["red shoes", "green pants", "tie", "belt"]

Check-in

With your teams discuss what could be the answer for the question below. Once you think you have the right answer, raise your hand and explain why you think the answer you have chosen is correct. If you get the correct answer, you get a reward :)

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

}

}

Why is this simulation considered an abstraction?

  • 1: It uses a conditional to execute one part of the code only when a particular condition is met.
  • 2: It uses a REPEAT loop to run the same block of code multiple times.
  • 3: It simplifies a real-world scenario into something that can be modeled in code and executed on a computer.
  • 4: It does not request input from the user or display output to the user.

Let's look at an example: Coin Flip

import random

def coinflip():         #def function 
    randomflip = random.randint(0, 1) #picks either 0 or 1 randomly (50/50 chance of either) 
    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
Heads
Tails
Heads

Activity: Change the code below so that instead of using a fair coin, a weighted coin is flipped. THis means there is no longer an equal chance that either heads of tails will be flipped. Let's say there is a 2/3 chance of heads and only 1/3 chance of tails.

Hint: maybe you can increase the range of integers?

 

Using Binary for Simulations

jiya

 

Explanation of ^^

Hacks

Shruthi, Noor, Ananya

Hack #1: Create an algorithm that will flip a dice

Hack #2: Using the questions bank below, create a quiz that presents the user a random question and calculates the user's score. You can use the template below or make your own. Making your own using a loop can give you extra point.

questions = 4
correct = 0


Q1 = input()
if Q1 == :
    

Q2 = input()
if Q2 == :
   
Q3 = input()
if Q3 == :
   

Q4 = input()




print( " you scored " + str(correct) +"/" + str(questions))

Hack #3: Create your own simulation based on a randomized real-world scenario. What would you want to simulate? (cannot be the coins, dice, closet or other previous examples)