Vocab

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

3.16: Simulations

  • 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.0101525445522108 seconds for an object that weighs 110 pounds to fall  5.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.0999999999999999
5.928999999999999

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  572 Turns
The Man Has Made It Home After  2 Turns
( 66 -404 )
The Man Has Made It Home After  1828044 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  1928 Turns
The Man Has Made It Home After  56 Turns
The Man Has Made It Home After  12 Turns
The Man Has Made It Home After  5768 Turns
The Man Has Made It Home After  22 Turns
( -14 328 )
( -615 2049 )
( -1159 2319 )
( -2036 1732 )
( -2585 1893 )
( -2833 2779 )
( -1702 2286 )
( -684 2366 )
( -1541 1373 )
( -2082 2754 )
Caped
The Man Has Made It Home After  2 Turns
( 778 -670 )
( 1112 -1570 )
( 1488 -1500 )
( 1136 -902 )
( 1621 -553 )
( 2546 -370 )
( 2531 -193 )
( 2797 -3 )
( 3104 -230 )
( 2032 1002 )
Caped
The Man Has Made It Home After  10 Turns
( 501 -1749 )
( 647 -2137 )
( 1308 -3072 )
( 2321 -3659 )
( 3291 -4465 )
( 2779 -4627 )
( 2292 -4508 )
( 2668 -4744 )
( 1801 -4825 )
( 2392 -3998 )
Caped
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  12 Turns
( -1235 1487 )
( -1977 2633 )
( -1229 2907 )
( -2113 4507 )
( -1013 5001 )
( -1899 3185 )
( -2019 2675 )
( -1339 2787 )
( -1675 4913 )
( -2086 4420 )
Caped
The Man Has Made It Home After  6 Turns
The Man Has Made It Home After  784 Turns
( -1105 811 )
( -802 574 )
( -1642 508 )
( -762 1000 )
( -586 1782 )
( -551 1287 )
( -873 1797 )
( -777 2045 )
( -710 2192 )
( -2326 1600 )
Caped
The Man Has Made It Home After  4 Turns
( 1001 675 )
( 888 -1056 )
( 101 -575 )
( -802 -1418 )
( -1621 -2107 )
( -131 -1483 )
( 723 -1063 )
( 825 -615 )
( 655 -691 )
( 518 -472 )
Caped
The Man Has Made It Home After  283134 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  14 Turns
The Man Has Made It Home After  8 Turns
The Man Has Made It Home After  18550 Turns
( -147 -637 )
( -497 -393 )
( -1147 -1043 )
( -1103 483 )
( -1463 -313 )
( -1695 -1027 )
( -1595 -585 )
( -1367 -573 )
( -1875 -987 )
( -2162 -640 )
Caped
The Man Has Made It Home After  36 Turns
( -244 262 )
( -1827 867 )
( -2695 2177 )
( -1045 1895 )
( -1242 2510 )
( -2110 3398 )
( -1437 3329 )
( -1826 3328 )
( -1898 2946 )
( -1169 2805 )
Caped
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  44 Turns
( -484 -44 )
( -333 -805 )
( -1934 -1484 )
( -2054 -730 )
( -2698 -486 )
( -2237 -683 )
( -2378 -1132 )
( -2729 -2369 )
( -3263 -2557 )
( -2990 -1460 )
Caped
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  28 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2336 Turns
The Man Has Made It Home After  6 Turns
The Man Has Made It Home After  10446 Turns
The Man Has Made It Home After  2 Turns
( -751 -289 )
( -1276 -398 )
( -944 -1130 )
( -390 -52 )
( -992 -1144 )
( -696 -338 )
( -1482 -720 )
( -2860 944 )
( -3373 1163 )
( -3846 -410 )
Caped
The Man Has Made It Home After  141810 Turns
The Man Has Made It Home After  1110 Turns
The Man Has Made It Home After  2 Turns
( -789 -585 )
( 993 223 )
( 1196 1154 )
( 1130 1598 )
( 1677 959 )
( 2182 -946 )
( 528 -526 )
( 443 173 )
( -504 498 )
( -1990 1058 )
Caped
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  6 Turns
( -509 117 )
( -91 -953 )
The Man Has Made It Home After  2556012 Turns
( 1371 -1267 )
( 2279 -1267 )
( 1937 -913 )
( 2078 -1288 )
( 2963 -367 )
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb Cell 9 in <cell line: 9>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=12'>13</a> if step == 1:
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=13'>14</a>     x = x-1
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14'>15</a> if step == 2:
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=15'>16</a>     y = y+1
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16'>17</a> if step == 3:

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)
( -397 616 )  |  -103
( 429 342 )  |  139
( 1148 819 )  |  -663
( 1579 1068 )  |  249
( 1296 1471 )  |  -549
( 1651 905 )  |  -836
( 663 962 )  |  -387
( -431 1555 )  |  -138
( -40 1225 )  |  -331
( -63 1059 )  |  350
Caped
( -77 -817 )  |  -30
( 286 -1176 )  |  286
( -510 -2666 )  |  744
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb Cell 10 in <cell line: 10>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a> turns = []
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=9'>10</a> while (nights < 100):
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10'>11</a>     #rando movement
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=11'>12</a>     step = random.randrange(6)
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=12'>13</a>     if step == 0:
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/shreyasapkal/shreya-2/_notebooks/2022-12-13-3.16-notes.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=13'>14</a>         x = x+1

File /usr/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 /usr/lib/python3.8/random.py:254, in Random._randbelow_with_getrandbits(self, n)
    251 "Return a random int in the range [0,n).  Raises ValueError if n==0."
    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:

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.