First Order of Business: Get your notebook

  • Open a terminal in vscode, run command: cd _notebooks, type 'wget' and paste this link into said terminal and run it

  • Take notes wherever you please, but you will be graded on participating

So, what is a simulation anyway?

  • A simulation is a tested scenario used for viewing results/outputs to prepare for them in real world situations

  • These can be used for games like dice rolling, spinners, etc

  • These can be used for practical things such as building structures, testing car crashes, and other things before engaging in them in the real world

  • These simulations can have the option of obeying real world physics (Gravity, collision) or they can go against these norms since this is a fictitious scenario, and couldn't happen in real life

Notes:

  • real world physics don't have to apply in simulations, but can be visualized if needed
  • allows people to test what would happen without causing damage

Big Question

  • Which of the following simulations could be the LEAST useful?

  • A retailer trying to identify which products sold the most

  • A restaurant determining the efficiency of robots
  • An insurance company studying the rain impact of cars
  • A sports bike company studying design changes to their new bike design
  • If you guessed a bike company, you're wrong, because the retail simulation was the right answer. Simulating robots in food service, studying rain impact on vehicles, and new bike design can contribute a lot more to society in comparison to seeing what products sell more than others.

Notes:

  • retail simulation is more niche???
  • therefore not as useful
  • more difficult to simulate a sail

Next Big Question

If you were making a simulation for making a new train station, which of the following would be true about this simulation?

  • It could reveal potential problems/safety issues before construction starts
  • It cannot be used to test the train station in different weather
  • Simulation will add high costs to projects
  • Simulation is not needed because this train station already exists
  • Potential Safety was the right answer, because you need somewhere to test the safety and ethnics of what you're about to do before you start building it. Otherwise, let's just say you'll have a special plaque for FBI's Most Wanted

Simulation 1:

Both programs below do the same thing. Given a height and a weight, they calculate how long it will take for a object to fall to the ground in a vacuum subjected to normal Earth levels of gravity.

However, the second one is a simulation. It calculates the distance the object has fallen every 0.1 seconds. This is useful for if you wanted a visual representation of a falling object, which pure math can't do as smoothly.

height = float(input("height in meters?"))

weight = input("weight in pounds?")

stuff = (2 * (height / 9.8))**(1/2)

print("It will take", stuff,"seconds for an object that weighs",weight,"pounds","to fall ",height,"meters in a vacuum")
It will take 1.1693361102674928 seconds for an object that weighs 45 pounds to fall  6.7 meters in a vacuum
t = 0
g = 0
d = 0
false = True
while false:
    t = t + 0.1
    d = 9.8 / 2 * (t**2)
    if d >= height:
        false = False
    #print(d) # if you want to print the distance every time it calculates it. Too long to output to a terminal, but this could be useful to display graphically. 
    #print(t)

print(t)
print(d)
1.2
7.056

Simulation 2:

  • This simulation is made in order to simulate movement on a 2d plane vs a 3d plane.

  • How it works: we have multiple variables, if statements and equations under a while command in order to randomy generate steps on a 2d plane. Once it reaches the set destination, it will say that the man made it home after x amount of steps.

  • For the 3D plane, it takes a lot longer due to how big and open the 3d environment is, so there are more if statements in the 3d plane

(explain further)

import random
x = 0
y = 0
nights = 0
turn = 0
stopped = 0
turns = []

while (nights < 100):
    step = random.randrange(4)
    if step == 0:
        x = x+1
    if step == 1:
        x = x-1
    if step == 2:
        y = y+1
    if step == 3:
        y = y-1

    turn = turn + 1

    if x == 0 and y == 0:
        nights = nights + 1
        print("The Man Has Made It Home After ", turn, "Turns")
        turns.append(turn)
        turn = 0
    if turn/1000 % 1000 == 0 and x + y != 0:
        print("(", x,y, ")")
    if (turn > 10000000):
        stopped = stopped + 1
        turn = 0
        x = 0
        y = 0
        nights = nights + 1
        print("Caped")

average = sum(turns) / len(turns)
print("Avaerage", average, "Ones that when't too long ", stopped)
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  259698 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  4 Turns
( 412 724 )
( 441 -259 )
( -787 -1941 )
( -2460 -1278 )
( -3682 -1520 )
( -3482 -418 )
( -3511 -397 )
( -3409 -71 )
( -3239 -667 )
( -2746 -1026 )
Caped
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  1966 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  30 Turns
The Man Has Made It Home After  26 Turns
( 971 167 )
( 404 136 )
( 1547 967 )
( 1532 794 )
( 2161 1057 )
( 925 1009 )
( 710 1788 )
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb Cell 10 in <cell line: 9>()
     <a href='vscode-notebook-cell:/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb#X12sZmlsZQ%3D%3D?line=10'>11</a> if step == 0:
     <a href='vscode-notebook-cell:/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb#X12sZmlsZQ%3D%3D?line=11'>12</a>     x = x+1
---> <a href='vscode-notebook-cell:/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb#X12sZmlsZQ%3D%3D?line=12'>13</a> if step == 1:
     <a href='vscode-notebook-cell:/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb#X12sZmlsZQ%3D%3D?line=13'>14</a>     x = x-1
     <a href='vscode-notebook-cell:/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb#X12sZmlsZQ%3D%3D?line=14'>15</a> if step == 2:

KeyboardInterrupt: 
import random
x = 0
y = 0
z = 0
nights = 0
turn = 0
stopped = 0
turns = []

while (nights < 100):
    #rando movement
    step = random.randrange(6)
    if step == 0:
        x = x+1
    if step == 1:
        x = x-1
    if step == 2:
        y = y+1
    if step == 3:
        y = y-1
    if step == 4:
        z = z+1
    if step == 5:
        z = z-1
    #Turn counter
    turn = turn + 1
    #Goal check
    if x == 0 and y == 0 and z == 0:
        nights = nights + 1
        print("The Bird Has Made It Home After ", turn, "Turns")
        turns.append(turn)
        turn = 0
    if turn/1000 % 1000 == 0 and x + y + z != 0:
        print("(", x,y, ") ","| ", z)
    #Too long Stoper
    if (turn > 10000000):
        stopped = stopped + 1
        turn = 0
        x = 0
        y = 0
        z = 0
        nights = nights + 1
        print("Caped")

average = sum(turns) / len(turns)
print("Avaerage", average,"Ones that when't too long ", stopped)
( -816 203 )  |  -583
( -1055 -952 )  |  -565
( -1910 -230 )  |  554
( -1559 -413 )  |  -364
( -867 -1264 )  |  -279
( -774 -1330 )  |  192
( -875 -1931 )  |  -154
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb Cell 11 in <cell line: 10>()
     <a href='vscode-notebook-cell:/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb#X13sZmlsZQ%3D%3D?line=30'>31</a>     turns.append(turn)
     <a href='vscode-notebook-cell:/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb#X13sZmlsZQ%3D%3D?line=31'>32</a>     turn = 0
---> <a href='vscode-notebook-cell:/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb#X13sZmlsZQ%3D%3D?line=32'>33</a> if turn/1000 % 1000 == 0 and x + y + z != 0:
     <a href='vscode-notebook-cell:/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb#X13sZmlsZQ%3D%3D?line=33'>34</a>     print("(", x,y, ") ","| ", z)
     <a href='vscode-notebook-cell:/Users/ekamjotkaire/csp/fast-pages/_notebooks/2022-12-12-lesson8.ipynb#X13sZmlsZQ%3D%3D?line=34'>35</a> #Too long Stoper

KeyboardInterrupt: 

Simulations in the wild

Simulations are used extremely frequently in real life applications. One of the most common examples of simulations are video games. A games physics engine can accurately simulate objects colliding

Another example is Blender, the software used in 3d animations class, here at Del Norte. Blender is made up of many small simulations, but one big one it uses is simulating the way light bounces off of and interacts with objects.

HW !!!

Create a simulation. It can be anything, just has to simulate something.

Some ideas:

  • Two objects colliding
  • Gravity on other planets

AND

Find an example of a simulation in a software/game you use, screenshot, and explain how it is a simulation

This is a simulation of the game hangman:

import sys

#picking a word:
print("player 1: pick a word")
word = input("").lower()
blank = "-" 

#defining variables
MAX_WRONG_GUESS = 5 #max amount of wrong guesses allowed
letters = list(word) #separating all letters into a separate index in a new list
wrong_guess = 0 #total amount of wrong guesses done

#empty list used to show progress.. anytime a correct letter is guessed, the dashes will change to that letter
guesses = [] 
for n in letters:
    guesses.append("-")
    
#procedure used to show board after every change
def progress():
    print("")
    print("current board")
    print(str(guesses))
        
            

#procedure for guessing a letter
def round():
    global wrong_guess #global makes it so that a variable defined earlier can also be used in this function
    global guesses
    print("player 2, pick a letter") #player picks letter
    choice = input("")
    print("your choice:", choice)
    n = 0 
    found = 0
    for a in letters:
        if choice == letters[n]:
            guesses[n] = choice
            found = 1 
        n = n + 1
    if ( found == 0 ): #if the guessed letter is not found in the word
        wrong_guess = wrong_guess + 1 
        print("that letter is not in this word!")
        print("")
    

while (wrong_guess < MAX_WRONG_GUESS ):
    progress()
    round()
    if blank not in guesses: #checks to see if the word has been correctly guessed
        print("game over, you guessed the word!")
        sys.exit() #this command will result in a traceback error, however exit() and quit() were not working
if ( wrong_guess >= MAX_WRONG_GUESS) : #if max amount of incorrect guesses allowed had been reached
    print("too many wrong guesses.. game over :(") 
    print("the correct word was:", word)  

        
player 1: pick a word

current board
['-', '-', '-']
player 2, pick a letter
your choice: f
that letter is not in this word!


current board
['-', '-', '-']
player 2, pick a letter
your choice: c

current board
['c', '-', '-']
player 2, pick a letter
your choice: a

current board
['c', 'a', '-']
player 2, pick a letter
your choice: t
game over, you guessed the word!
An exception has occurred, use %tb to see the full traceback.

SystemExit

**I could not figure out how to exit the code without using SystemExit, which results in a traceback error

Example of a simulation in a video game:

This is a screenshot from a video game, where you have to hit the ball with the stick across the net. This counts as a simulation, because the game predicts where the ball would land in real life based on the angle that you hit the ball in. On top of that, the ball bounces and interacts with the environment, which are more examples of a simulation.