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 0.4517539514526256 seconds for an object that weighs 1 pounds to fall  1.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)

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  2 Turns
The Man Has Made It Home After  242 Turns
The Man Has Made It Home After  1104 Turns
The Man Has Made It Home After  8 Turns
The Man Has Made It Home After  252 Turns
The Man Has Made It Home After  72 Turns
The Man Has Made It Home After  6 Turns
( -433 -177 )
( -980 352 )
( -2076 644 )
( -2967 1767 )
( -1860 418 )
( -3097 701 )
( -3413 1203 )
( -2200 1040 )
( -3453 -323 )
( -3768 -650 )
Caped
The Man Has Made It Home After  66 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  2 Turns
The Man Has Made It Home After  26 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  3226 Turns
( -2003 -1571 )
( -1772 572 )
( -1920 64 )
( -2421 283 )
( -2743 -387 )
( -2816 -1574 )
( -2482 -1758 )
( -2345 -1217 )
( -2839 -1531 )
( -3143 -2673 )
Caped
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  38 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  2 Turns
( 152 -36 )
( -886 758 )
( -1492 254 )
( -1674 506 )
( -1742 -260 )
( -1527 1071 )
( -1778 1190 )
( -2104 1398 )
( -3419 2061 )
( -3453 2199 )
Caped
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  196 Turns
The Man Has Made It Home After  24 Turns
The Man Has Made It Home After  43918 Turns
The Man Has Made It Home After  3250 Turns
The Man Has Made It Home After  10 Turns
( 260 -302 )
The Man Has Made It Home After  1435704 Turns
The Man Has Made It Home After  8 Turns
The Man Has Made It Home After  12 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  9576 Turns
The Man Has Made It Home After  6 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  34 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  4826 Turns
( 420 952 )
( -181 1257 )
( -549 1561 )
( -837 1731 )
( -1647 751 )
( -1994 1794 )
( -1643 2657 )
( -1793 3483 )
( -1296 3288 )
( -1046 3224 )
Caped
( 515 301 )
( 398 702 )
( 1403 1239 )
( 1704 1474 )
( 2642 1146 )
( 2893 2009 )
( 2974 2422 )
( 3010 2506 )
( 3321 2619 )
( 3051 2213 )
Caped
( 276 -96 )
( 568 40 )
( -452 208 )
( -703 -499 )
( -900 -950 )
( -877 -229 )
( -1173 -469 )
( -297 -237 )
The Man Has Made It Home After  8474140 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  2 Turns
( 332 -268 )
( -579 -1183 )
( 921 -1685 )
( 1338 -1394 )
( 1207 -2007 )
( 1160 -2348 )
( 530 -3690 )
( 1903 -3183 )
( 445 -3119 )
( -753 -3789 )
Caped
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  230 Turns
( -949 -577 )
( -1917 325 )
( -446 440 )
( 278 502 )
( -922 382 )
( -419 -1627 )
( -682 -912 )
( -1997 -413 )
( -192 -830 )
( 309 -1177 )
Caped
The Man Has Made It Home After  98 Turns
The Man Has Made It Home After  22 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  94464 Turns
The Man Has Made It Home After  14 Turns
The Man Has Made It Home After  6902 Turns
The Man Has Made It Home After  4 Turns
( 140 -572 )
( -102 114 )
The Man Has Made It Home After  2933872 Turns
The Man Has Made It Home After  2 Turns
( 462 1596 )
( 299 807 )
( -629 373 )
( -1554 -398 )
( -1197 -229 )
( -1638 412 )
( -1445 431 )
( -1756 694 )
( -652 342 )
( -524 268 )
Caped
( 80 578 )
( -843 715 )
( -1059 101 )
( -885 743 )
( -113 721 )
( -1565 1535 )
( -2254 688 )
( -2469 965 )
( -4008 824 )
( -3755 1215 )
Caped
( -191 -2153 )
( -104 -1092 )
( -72 -940 )
( 774 -1536 )
( 88 -1348 )
( -1247 -341 )
( -944 146 )
( -1594 112 )
( -2059 469 )
( -819 1035 )
Caped
( 75 -1345 )
( -530 -712 )
( -1646 -1282 )
( -2029 -1435 )
( -599 -1973 )
( -23 -1863 )
( -1300 -2174 )
( -1740 -2608 )
( -1996 -1472 )
( -1862 -1522 )
Caped
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  784 Turns
( -1587 -1415 )
( -1254 -1516 )
( -1589 -1551 )
( -1695 -2047 )
( -2194 -1582 )
( -2952 -2630 )
( -3136 -3426 )
( -2622 -2648 )
( -2640 -2884 )
( -2657 -2695 )
Caped
The Man Has Made It Home After  78 Turns
The Man Has Made It Home After  322148 Turns
The Man Has Made It Home After  32396 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  54 Turns
The Man Has Made It Home After  916 Turns
The Man Has Made It Home After  18 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  30 Turns
The Man Has Made It Home After  226 Turns
The Man Has Made It Home After  32 Turns
The Man Has Made It Home After  26 Turns
( -393 -257 )
( -847 -119 )
( -1387 -783 )
( -2900 130 )
( -1599 -77 )
( -219 -625 )
( -871 -747 )
( -1722 -556 )
( -1682 -822 )
( 155 -787 )
Caped
( 278 -482 )
( 812 -444 )
( 1309 -1241 )
( 1620 -1882 )
( 628 -1866 )
( 827 -2755 )
( -78 -2066 )
( -552 -1990 )
( -870 -822 )
( -247 181 )
Caped
The Man Has Made It Home After  44 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  12 Turns
The Man Has Made It Home After  12 Turns
The Man Has Made It Home After  8 Turns
( 230 1074 )
( 648 -330 )
( 826 -446 )
( 1185 -1139 )
( 987 -61 )
( 1461 53 )
( 2394 -1044 )
( 1977 -2261 )
( 2329 -1317 )
( 1306 -2010 )
Caped
The Man Has Made It Home After  7158 Turns
The Man Has Made It Home After  6 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  10 Turns
The Man Has Made It Home After  6 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  666 Turns
Avaerage 157377.3411764706 Ones that when't too long  15
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)

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

Running the sims make it slow to commit

Sim #1 updates 2 variables by a random amount at each iteration (runs for 100 iterations)

import random

# Define the number of iterations
num_iterations = 100

# Set the initial values of the variables
value_1 = 0
value_2 = 0

# Define the update function
def update_values():
  global value_1
  global value_2
  value_1 += random.randint(-1, 1)
  value_2 += random.randint(-1, 1)

# Run the simulation for the specified number of iterations
for i in range(num_iterations):
  update_values()
  print(f"Iteration {i}: value_1 = {value_1}, value_2 = {value_2}")

Sim #2 an object falling under the influence of gravity

import matplotlib.pyplot as plt

# Define the initial conditions
x = 0
y = 0
vx = 0
vy = 0
g = 9.8

# Set the time step and number of iterations
dt = 0.1
num_iterations = 100

# Define the update function
def update_position():
  global x, y, vx, vy
  x += vx * dt
  y += vy * dt
  vy -= g * dt

# Run the simulation for the specified number of iterations
for i in range(num_iterations):
  update_position()

# Plot the results
plt.plot(x, y)
plt.show()

Example of simulation

GTA Character Editor

pc