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

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, sudying 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.

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 Saftey was the right answer, because you need somewhere to test the safety and ethicness 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.4285714285714286 seconds for an object that weighs 50 pounds to fall  10.0 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.5000000000000002
11.025000000000002

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
( 440 654 )
( 1574 1142 )
( 1988 1484 )
( 2274 2238 )
( 1424 2272 )
( 913 3295 )
( 509 3495 )
( 261 3657 )
( -400 3144 )
( -1981 3347 )
Caped
The Man Has Made It Home After  44846 Turns
The Man Has Made It Home After  4 Turns
( -905 287 )
( -966 354 )
( -538 12 )
( -319 -173 )
( -253 -321 )
( 33 -711 )
The Man Has Made It Home After  6716252 Turns
( 393 447 )
( 261 605 )
The Man Has Made It Home After  2691676 Turns
( -673 -149 )
( 212 784 )
( 512 568 )
( 1040 1556 )
( 1247 2715 )
( 602 1788 )
( 154 2014 )
( -166 2212 )
( -917 2507 )
( -2085 3059 )
Caped
The Man Has Made It Home After  654 Turns
The Man Has Made It Home After  680 Turns
( -22 -652 )
( 769 -147 )
( 780 -70 )
( 522 -94 )
The Man Has Made It Home After  4893306 Turns
( 791 121 )
( 710 -890 )
( 1192 -792 )
( 2243 -531 )
( 3484 220 )
( 3768 444 )
( 4918 1976 )
( 4889 2721 )
( 4359 3667 )
( 4854 4236 )
Caped
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  18 Turns
The Man Has Made It Home After  124 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  6 Turns
The Man Has Made It Home After  506 Turns
The Man Has Made It Home After  4852 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  401730 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  276 Turns
The Man Has Made It Home After  12 Turns
The Man Has Made It Home After  18 Turns
( 427 143 )
( 542 -620 )
( 989 -1691 )
( 2234 -1692 )
( 1859 -2113 )
( 1269 -2511 )
( 592 -2206 )
( 1028 -2328 )
( 1025 -3321 )
( 588 -2416 )
Caped
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb Cell 10 in <cell line: 9>()
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X12sZmlsZQ%3D%3D?line=17'>18</a>     y = y-1
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X12sZmlsZQ%3D%3D?line=19'>20</a> turn = turn + 1
---> <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X12sZmlsZQ%3D%3D?line=21'>22</a> if x == 0 and y == 0:
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X12sZmlsZQ%3D%3D?line=22'>23</a>     nights = nights + 1
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X12sZmlsZQ%3D%3D?line=23'>24</a>     print("The Man Has Made It Home After ", turn, "Turns")

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)
The Bird Has Made It Home After  2 Turns
The Bird Has Made It Home After  2 Turns
The Bird Has Made It Home After  2 Turns
( -185 525 )  |  380
( -293 379 )  |  498
( -534 1074 )  |  828
( 176 613 )  |  567
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb Cell 11 in <cell line: 10>()
      <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X13sZmlsZQ%3D%3D?line=7'>8</a> turns = []
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X13sZmlsZQ%3D%3D?line=9'>10</a> while (nights < 100):
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X13sZmlsZQ%3D%3D?line=10'>11</a>     #rando movement
---> <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X13sZmlsZQ%3D%3D?line=11'>12</a>     step = random.randrange(6)
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X13sZmlsZQ%3D%3D?line=12'>13</a>     if step == 0:
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X13sZmlsZQ%3D%3D?line=13'>14</a>         x = x+1

File /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/random.py:215, in Random.randrange(self, start, stop, step, _int)
    213 if stop is None:
    214     if istart > 0:
--> 215         return self._randbelow(istart)
    216     raise ValueError("empty range for randrange()")
    218 # stop argument supplied.

File /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/random.py:255, in Random._randbelow_with_getrandbits(self, n)
    253 getrandbits = self.getrandbits
    254 k = n.bit_length()  # don't use (n-1) here because n can be 1
--> 255 r = getrandbits(k)          # 0 <= r < 2**k
    256 while r >= n:
    257     r = getrandbits(k)

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

import time

# Define a function that updates the simulation
def update_simulation():
  # Update the state of the simulation here...
  
  # Prompt the user to enter the mass and radius of a planet
  mass = float(input("Enter the mass of the planet (in kilograms): "))
  radius = float(input("Enter the radius of the planet (in kilometers): "))
  
  # Calculate the gravity of the planet
  gravity = (6.67 * 10**-11 * mass) / (radius**2)
  print(f"Gravity of planet: {gravity}")
  #Gravity of planet 1: Neptune
  #Gravity of planet 2: Saturn
  #Gravity of planet 3: Jupiter
  #Gravity of planet 4: Uranus

# Run the simulation indefinitely
while True:
  update_simulation()
  time.sleep(1) # Pause for 1 second between each update
Gravity of planet: 54212767.98021029
Gravity of planet: 28951451.382520344
Gravity of planet: 67084535.02285952
Gravity of planet: 23315153.5014559
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb Cell 15 in <cell line: 17>()
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=14'>15</a> # Run the simulation indefinitely
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=15'>16</a> while True:
---> <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=16'>17</a>   update_simulation()
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=17'>18</a>   time.sleep(1)

/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb Cell 15 in update_simulation()
      <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=3'>4</a> def update_simulation():
      <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=4'>5</a>   # Update the state of the simulation here...
      <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=5'>6</a>   
      <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=6'>7</a>   # Prompt the user to enter the mass and radius of a planet
----> <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=7'>8</a>   mass = float(input("Enter the mass of the planet (in kilograms): "))
      <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=8'>9</a>   radius = float(input("Enter the radius of the planet (in kilometers): "))
     <a href='vscode-notebook-cell:/Users/dmsol/Tera/_notebooks/2022-12-13-Studentlesson8.ipynb#X22sZmlsZQ%3D%3D?line=10'>11</a>   # Calculate the gravity of the planet

ValueError: could not convert string to float: ''

Example of a Simulation

In terms of coding, NBA 2K is a simulation because it uses a computer program to simulate the behavior and interactions of the various elements that make up a game of basketball. This includes the players, the ball, the court, and the rules of the game. To create this simulation, the developers of NBA 2K use a combination of algorithms, data structures, and mathematical calculations to model the behavior of these elements. For example, they may use algorithms to simulate the movement of the ball and the players on the court, and use data structures to store information about the teams, players, and game rules. Additionally, the developers may use mathematical calculations to model the physics of the game, such as the effects of gravity on the ball and the players, and the interactions between different objects on the court.