August 2023 to June 2024
View the Project on GitHub IshanCornick/new_student
A simulation is the use of a computer software to represent the dynamic responses of one system by the behaviour of another system modeled after it. A simulation uses a mathematical descriptions, or models, of a real system in the form of a computer program.
Simulation are absractions of more complex objects or phenomena for a specific purpose
Simulations utilize varying sets of values to reflect the changings states of a phenomenon
Simulations work best when the real world experemnts are too impractical or time consuming. For example, simulating how different cars behave when they crash, would be much better than crashng actual cars in the real world, which would be expensive and dangerous.
Simulating something like a dice roll in real life would require accounting for things like: weight, flaws in design, thrust, and gravity.
- KEEP IT SIMPLE! just use a random-number generator! Ignore minor causes of variablility
#imports random module so we can use it in our code
import random
#sets variable random_number as a random number between 1 and 100
random_number = random.randint(1, 100)
#Printing out your random Number
print(random_number)
import random
def flip_coin():
return random.choice(["Heads", "Tails"])
def coin_flip_simulation(num_flips):
heads_count = 0
tails_count = 0
for _ in range(num_flips):
result = flip_coin()
if result == "Heads":
heads_count += 1
else:
tails_count += 1
return heads_count, tails_count
if __name__ == "__main__":
num_flips = 1000 #This is the number of coin flips you want to simulate
heads, tails = coin_flip_simulation(num_flips)
print("Number of Heads: "+ str(heads))
print("Number of Tails: " + str(tails))
print("Heads Probability: "+ str({heads / num_flips}))
print("Tails Probability: "+ str({tails / num_flips}))
Utilize “random” to create a basic simulation of a rolling TWO dice. Print the sum of both dice rolls. Remember to practice good syntax when naming your variables.
import random
dice1 = random.randint(0,6)
dice2 = random.randint(0,6)
print(dice1+dice2)
#Code, Code, Code
5
Simulations often utilize algorithms and equations to perform tasks because simulations don’t always have the same output
- the output of a simulation depends on the input
An algorithm is a finite sequence of instructions used to solve problems or perform computations.
- commonly used alongside functions
#Defining Function
def algorithm(input):
#Manipulating input and preparing it for the output.
output = input+2
#Return the output
return output
#Call the Function to start the algorithm
algorithm(5)
7
Simulate how long an object will fall for using an algorithm, with user-inputed variables for height dropped. Use the following formula as a reference.
# Constant, Acceleration due to gravity (m/s^2)
import math
G = 9.81
def simulation(h):
t = (2*h/G)**1/2
return t
print(simulation(100))
10.19367991845056
For loops can also be used in simulations
- They can simulate events that repeat but don’t always have the same output
# Example For Loop
#Creating For Loop to repeat 4 times
for i in range(4):
#Action that happens inside for loop
print("This is run number: " + str(i))
This is run number: 0
This is run number: 1
This is run number: 2
This is run number: 3
You are gambling addict (sigma).
Each session you roll 2 dice.
If your dice roll is greater than or equal to 9 you win the session.
If you win over 5 sessions, you win the jackpot.
Simulate your odds to predict if you will hit the jackpot (how many rounds did you win?) using a for loop and random.
import random
import math
sessions_won = 0
jackpots_hit = 0
for i in range (200):
num = random.randint(1, 6) + random.randint(1, 6)
if num >= 9:
sessions_won +=1
if sessions_won%5==0:
jackpots_hit +=1
print ("You won "+ str(sessions_won) + " sessions.")
print ("You won "+ str(jackpots_hit) + " jackpots.")
chance1 = sessions_won/200
chance2 = jackpots_hit/40
print ("Chance to win a session is " + str(sessions_won)+"/200")
print ("Chance to win a jackpot after 5 sessions is " + str(jackpots_hit)+"/40")
You won 53 sessions.
You won 32 jackpots.
Chance to win a session is 53/200
Chance to win a jackpot after 5 sessions is 32/200
Welcome to Flight Simulator! Your goal is to complete a Python program that simulates a flight We’ve set up some initial values for altitude, speed, and fuel. Your task is to update these values to make the flight more realistic.
import random
print("Welcome to Flight Simulator!")
altitude = 0
speed = 100
fuel = 1000
max_altitude = 10000
min_speed = 60
fuel_consumption_rate = 1
while altitude < max_altitude and fuel > 0:
altitude_change = random.randint(0, 100)
speed_change = random.randint(-10, 10)
fuel_change = random.randint(-5, -1)
altitude += altitude_change
speed += speed_change
if speed < min_speed:
speed = min_speed
fuel += fuel_change
if fuel < 0:
fuel = 0
print(f"Altitude: {altitude} feet, Speed: {speed} knots, Fuel: {fuel} gallons")
if altitude >= max_altitude:
print("Flight reached 10,000 feet. Mission accomplished!")
else:
print("Out of fuel. Flight mission failed.")
Welcome to Flight Simulator!
Altitude: 6 feet, Speed: 94 knots, Fuel: 996 gallons
Altitude: 100 feet, Speed: 104 knots, Fuel: 991 gallons
Altitude: 139 feet, Speed: 100 knots, Fuel: 987 gallons
Altitude: 219 feet, Speed: 102 knots, Fuel: 982 gallons
Altitude: 258 feet, Speed: 98 knots, Fuel: 981 gallons
Altitude: 357 feet, Speed: 100 knots, Fuel: 977 gallons
Altitude: 421 feet, Speed: 105 knots, Fuel: 976 gallons
Altitude: 516 feet, Speed: 106 knots, Fuel: 974 gallons
Altitude: 600 feet, Speed: 109 knots, Fuel: 969 gallons
Altitude: 655 feet, Speed: 102 knots, Fuel: 965 gallons
Altitude: 750 feet, Speed: 99 knots, Fuel: 963 gallons
Altitude: 849 feet, Speed: 89 knots, Fuel: 959 gallons
Altitude: 913 feet, Speed: 87 knots, Fuel: 954 gallons
Altitude: 994 feet, Speed: 89 knots, Fuel: 952 gallons
Altitude: 1037 feet, Speed: 88 knots, Fuel: 947 gallons
Altitude: 1096 feet, Speed: 82 knots, Fuel: 944 gallons
Altitude: 1125 feet, Speed: 78 knots, Fuel: 943 gallons
Altitude: 1151 feet, Speed: 73 knots, Fuel: 938 gallons
Altitude: 1243 feet, Speed: 80 knots, Fuel: 937 gallons
Altitude: 1342 feet, Speed: 83 knots, Fuel: 934 gallons
Altitude: 1401 feet, Speed: 77 knots, Fuel: 933 gallons
Altitude: 1423 feet, Speed: 77 knots, Fuel: 929 gallons
Altitude: 1459 feet, Speed: 70 knots, Fuel: 927 gallons
Altitude: 1469 feet, Speed: 67 knots, Fuel: 924 gallons
Altitude: 1512 feet, Speed: 62 knots, Fuel: 922 gallons
Altitude: 1577 feet, Speed: 65 knots, Fuel: 917 gallons
Altitude: 1649 feet, Speed: 60 knots, Fuel: 915 gallons
Altitude: 1726 feet, Speed: 60 knots, Fuel: 911 gallons
Altitude: 1769 feet, Speed: 65 knots, Fuel: 909 gallons
Altitude: 1805 feet, Speed: 70 knots, Fuel: 906 gallons
Altitude: 1845 feet, Speed: 73 knots, Fuel: 901 gallons
Altitude: 1925 feet, Speed: 70 knots, Fuel: 896 gallons
Altitude: 2000 feet, Speed: 67 knots, Fuel: 891 gallons
Altitude: 2031 feet, Speed: 65 knots, Fuel: 888 gallons
Altitude: 2040 feet, Speed: 64 knots, Fuel: 887 gallons
Altitude: 2087 feet, Speed: 73 knots, Fuel: 882 gallons
Altitude: 2128 feet, Speed: 81 knots, Fuel: 877 gallons
Altitude: 2160 feet, Speed: 88 knots, Fuel: 874 gallons
Altitude: 2187 feet, Speed: 92 knots, Fuel: 869 gallons
Altitude: 2229 feet, Speed: 99 knots, Fuel: 864 gallons
Altitude: 2260 feet, Speed: 97 knots, Fuel: 859 gallons
Altitude: 2271 feet, Speed: 89 knots, Fuel: 856 gallons
Altitude: 2306 feet, Speed: 95 knots, Fuel: 851 gallons
Altitude: 2317 feet, Speed: 104 knots, Fuel: 847 gallons
Altitude: 2333 feet, Speed: 97 knots, Fuel: 843 gallons
Altitude: 2426 feet, Speed: 88 knots, Fuel: 840 gallons
Altitude: 2516 feet, Speed: 97 knots, Fuel: 837 gallons
Altitude: 2571 feet, Speed: 90 knots, Fuel: 833 gallons
Altitude: 2654 feet, Speed: 100 knots, Fuel: 832 gallons
Altitude: 2655 feet, Speed: 90 knots, Fuel: 831 gallons
Altitude: 2749 feet, Speed: 98 knots, Fuel: 828 gallons
Altitude: 2781 feet, Speed: 93 knots, Fuel: 827 gallons
Altitude: 2802 feet, Speed: 94 knots, Fuel: 825 gallons
Altitude: 2820 feet, Speed: 84 knots, Fuel: 824 gallons
Altitude: 2821 feet, Speed: 87 knots, Fuel: 822 gallons
Altitude: 2823 feet, Speed: 89 knots, Fuel: 820 gallons
Altitude: 2916 feet, Speed: 98 knots, Fuel: 816 gallons
Altitude: 3016 feet, Speed: 103 knots, Fuel: 815 gallons
Altitude: 3057 feet, Speed: 95 knots, Fuel: 812 gallons
Altitude: 3081 feet, Speed: 90 knots, Fuel: 807 gallons
Altitude: 3153 feet, Speed: 99 knots, Fuel: 804 gallons
Altitude: 3242 feet, Speed: 91 knots, Fuel: 801 gallons
Altitude: 3338 feet, Speed: 93 knots, Fuel: 797 gallons
Altitude: 3379 feet, Speed: 96 knots, Fuel: 796 gallons
Altitude: 3395 feet, Speed: 100 knots, Fuel: 795 gallons
Altitude: 3422 feet, Speed: 110 knots, Fuel: 793 gallons
Altitude: 3511 feet, Speed: 118 knots, Fuel: 791 gallons
Altitude: 3596 feet, Speed: 118 knots, Fuel: 788 gallons
Altitude: 3643 feet, Speed: 123 knots, Fuel: 783 gallons
Altitude: 3673 feet, Speed: 121 knots, Fuel: 782 gallons
Altitude: 3723 feet, Speed: 131 knots, Fuel: 780 gallons
Altitude: 3728 feet, Speed: 139 knots, Fuel: 778 gallons
Altitude: 3740 feet, Speed: 148 knots, Fuel: 774 gallons
Altitude: 3791 feet, Speed: 144 knots, Fuel: 771 gallons
Altitude: 3867 feet, Speed: 139 knots, Fuel: 766 gallons
Altitude: 3873 feet, Speed: 148 knots, Fuel: 765 gallons
Altitude: 3895 feet, Speed: 150 knots, Fuel: 764 gallons
Altitude: 3918 feet, Speed: 150 knots, Fuel: 761 gallons
Altitude: 3954 feet, Speed: 142 knots, Fuel: 760 gallons
Altitude: 4048 feet, Speed: 152 knots, Fuel: 756 gallons
Altitude: 4106 feet, Speed: 155 knots, Fuel: 752 gallons
Altitude: 4201 feet, Speed: 153 knots, Fuel: 749 gallons
Altitude: 4254 feet, Speed: 146 knots, Fuel: 744 gallons
Altitude: 4302 feet, Speed: 140 knots, Fuel: 742 gallons
Altitude: 4367 feet, Speed: 134 knots, Fuel: 739 gallons
Altitude: 4451 feet, Speed: 134 knots, Fuel: 736 gallons
Altitude: 4484 feet, Speed: 134 knots, Fuel: 733 gallons
Altitude: 4492 feet, Speed: 131 knots, Fuel: 730 gallons
Altitude: 4552 feet, Speed: 128 knots, Fuel: 726 gallons
Altitude: 4622 feet, Speed: 138 knots, Fuel: 724 gallons
Altitude: 4636 feet, Speed: 143 knots, Fuel: 723 gallons
Altitude: 4640 feet, Speed: 152 knots, Fuel: 721 gallons
Altitude: 4644 feet, Speed: 145 knots, Fuel: 719 gallons
Altitude: 4728 feet, Speed: 150 knots, Fuel: 716 gallons
Altitude: 4755 feet, Speed: 143 knots, Fuel: 714 gallons
Altitude: 4803 feet, Speed: 141 knots, Fuel: 709 gallons
Altitude: 4816 feet, Speed: 147 knots, Fuel: 704 gallons
Altitude: 4859 feet, Speed: 149 knots, Fuel: 699 gallons
Altitude: 4916 feet, Speed: 141 knots, Fuel: 694 gallons
Altitude: 5009 feet, Speed: 149 knots, Fuel: 693 gallons
Altitude: 5053 feet, Speed: 150 knots, Fuel: 692 gallons
Altitude: 5070 feet, Speed: 152 knots, Fuel: 691 gallons
Altitude: 5081 feet, Speed: 159 knots, Fuel: 690 gallons
Altitude: 5117 feet, Speed: 157 knots, Fuel: 687 gallons
Altitude: 5157 feet, Speed: 151 knots, Fuel: 683 gallons
Altitude: 5200 feet, Speed: 159 knots, Fuel: 682 gallons
Altitude: 5247 feet, Speed: 151 knots, Fuel: 681 gallons
Altitude: 5312 feet, Speed: 146 knots, Fuel: 676 gallons
Altitude: 5316 feet, Speed: 141 knots, Fuel: 673 gallons
Altitude: 5347 feet, Speed: 135 knots, Fuel: 670 gallons
Altitude: 5437 feet, Speed: 130 knots, Fuel: 669 gallons
Altitude: 5496 feet, Speed: 126 knots, Fuel: 666 gallons
Altitude: 5505 feet, Speed: 126 knots, Fuel: 664 gallons
Altitude: 5546 feet, Speed: 123 knots, Fuel: 661 gallons
Altitude: 5643 feet, Speed: 133 knots, Fuel: 660 gallons
Altitude: 5670 feet, Speed: 127 knots, Fuel: 658 gallons
Altitude: 5732 feet, Speed: 118 knots, Fuel: 654 gallons
Altitude: 5820 feet, Speed: 109 knots, Fuel: 650 gallons
Altitude: 5877 feet, Speed: 101 knots, Fuel: 648 gallons
Altitude: 5931 feet, Speed: 110 knots, Fuel: 643 gallons
Altitude: 5959 feet, Speed: 115 knots, Fuel: 638 gallons
Altitude: 6049 feet, Speed: 119 knots, Fuel: 637 gallons
Altitude: 6076 feet, Speed: 129 knots, Fuel: 632 gallons
Altitude: 6148 feet, Speed: 125 knots, Fuel: 629 gallons
Altitude: 6208 feet, Speed: 115 knots, Fuel: 625 gallons
Altitude: 6252 feet, Speed: 106 knots, Fuel: 622 gallons
Altitude: 6298 feet, Speed: 115 knots, Fuel: 618 gallons
Altitude: 6355 feet, Speed: 125 knots, Fuel: 617 gallons
Altitude: 6444 feet, Speed: 123 knots, Fuel: 613 gallons
Altitude: 6487 feet, Speed: 116 knots, Fuel: 610 gallons
Altitude: 6497 feet, Speed: 106 knots, Fuel: 605 gallons
Altitude: 6500 feet, Speed: 97 knots, Fuel: 604 gallons
Altitude: 6502 feet, Speed: 102 knots, Fuel: 600 gallons
Altitude: 6555 feet, Speed: 100 knots, Fuel: 597 gallons
Altitude: 6579 feet, Speed: 109 knots, Fuel: 592 gallons
Altitude: 6667 feet, Speed: 112 knots, Fuel: 588 gallons
Altitude: 6683 feet, Speed: 109 knots, Fuel: 587 gallons
Altitude: 6752 feet, Speed: 116 knots, Fuel: 586 gallons
Altitude: 6852 feet, Speed: 123 knots, Fuel: 584 gallons
Altitude: 6856 feet, Speed: 122 knots, Fuel: 583 gallons
Altitude: 6928 feet, Speed: 131 knots, Fuel: 582 gallons
Altitude: 6931 feet, Speed: 127 knots, Fuel: 578 gallons
Altitude: 6947 feet, Speed: 125 knots, Fuel: 576 gallons
Altitude: 7008 feet, Speed: 134 knots, Fuel: 575 gallons
Altitude: 7056 feet, Speed: 144 knots, Fuel: 570 gallons
Altitude: 7140 feet, Speed: 150 knots, Fuel: 569 gallons
Altitude: 7189 feet, Speed: 147 knots, Fuel: 566 gallons
Altitude: 7221 feet, Speed: 152 knots, Fuel: 561 gallons
Altitude: 7247 feet, Speed: 144 knots, Fuel: 558 gallons
Altitude: 7293 feet, Speed: 149 knots, Fuel: 554 gallons
Altitude: 7380 feet, Speed: 142 knots, Fuel: 551 gallons
Altitude: 7459 feet, Speed: 146 knots, Fuel: 549 gallons
Altitude: 7462 feet, Speed: 156 knots, Fuel: 544 gallons
Altitude: 7520 feet, Speed: 152 knots, Fuel: 543 gallons
Altitude: 7594 feet, Speed: 158 knots, Fuel: 542 gallons
Altitude: 7659 feet, Speed: 148 knots, Fuel: 540 gallons
Altitude: 7731 feet, Speed: 156 knots, Fuel: 535 gallons
Altitude: 7732 feet, Speed: 164 knots, Fuel: 533 gallons
Altitude: 7755 feet, Speed: 168 knots, Fuel: 531 gallons
Altitude: 7771 feet, Speed: 175 knots, Fuel: 527 gallons
Altitude: 7788 feet, Speed: 175 knots, Fuel: 522 gallons
Altitude: 7815 feet, Speed: 176 knots, Fuel: 521 gallons
Altitude: 7819 feet, Speed: 180 knots, Fuel: 517 gallons
Altitude: 7823 feet, Speed: 181 knots, Fuel: 514 gallons
Altitude: 7850 feet, Speed: 190 knots, Fuel: 509 gallons
Altitude: 7872 feet, Speed: 188 knots, Fuel: 506 gallons
Altitude: 7941 feet, Speed: 195 knots, Fuel: 501 gallons
Altitude: 7981 feet, Speed: 200 knots, Fuel: 498 gallons
Altitude: 8042 feet, Speed: 210 knots, Fuel: 494 gallons
Altitude: 8046 feet, Speed: 219 knots, Fuel: 489 gallons
Altitude: 8114 feet, Speed: 229 knots, Fuel: 484 gallons
Altitude: 8118 feet, Speed: 239 knots, Fuel: 483 gallons
Altitude: 8200 feet, Speed: 247 knots, Fuel: 478 gallons
Altitude: 8238 feet, Speed: 246 knots, Fuel: 473 gallons
Altitude: 8241 feet, Speed: 255 knots, Fuel: 471 gallons
Altitude: 8330 feet, Speed: 246 knots, Fuel: 470 gallons
Altitude: 8388 feet, Speed: 244 knots, Fuel: 466 gallons
Altitude: 8450 feet, Speed: 241 knots, Fuel: 465 gallons
Altitude: 8464 feet, Speed: 235 knots, Fuel: 461 gallons
Altitude: 8472 feet, Speed: 237 knots, Fuel: 460 gallons
Altitude: 8507 feet, Speed: 246 knots, Fuel: 456 gallons
Altitude: 8532 feet, Speed: 244 knots, Fuel: 452 gallons
Altitude: 8580 feet, Speed: 251 knots, Fuel: 447 gallons
Altitude: 8636 feet, Speed: 241 knots, Fuel: 443 gallons
Altitude: 8698 feet, Speed: 245 knots, Fuel: 439 gallons
Altitude: 8791 feet, Speed: 245 knots, Fuel: 435 gallons
Altitude: 8826 feet, Speed: 252 knots, Fuel: 431 gallons
Altitude: 8860 feet, Speed: 246 knots, Fuel: 430 gallons
Altitude: 8886 feet, Speed: 252 knots, Fuel: 427 gallons
Altitude: 8969 feet, Speed: 251 knots, Fuel: 424 gallons
Altitude: 9011 feet, Speed: 255 knots, Fuel: 420 gallons
Altitude: 9018 feet, Speed: 264 knots, Fuel: 416 gallons
Altitude: 9036 feet, Speed: 257 knots, Fuel: 412 gallons
Altitude: 9056 feet, Speed: 258 knots, Fuel: 408 gallons
Altitude: 9145 feet, Speed: 260 knots, Fuel: 404 gallons
Altitude: 9203 feet, Speed: 261 knots, Fuel: 402 gallons
Altitude: 9251 feet, Speed: 269 knots, Fuel: 397 gallons
Altitude: 9337 feet, Speed: 264 knots, Fuel: 392 gallons
Altitude: 9353 feet, Speed: 264 knots, Fuel: 390 gallons
Altitude: 9444 feet, Speed: 258 knots, Fuel: 389 gallons
Altitude: 9494 feet, Speed: 252 knots, Fuel: 387 gallons
Altitude: 9556 feet, Speed: 253 knots, Fuel: 384 gallons
Altitude: 9644 feet, Speed: 259 knots, Fuel: 381 gallons
Altitude: 9717 feet, Speed: 260 knots, Fuel: 377 gallons
Altitude: 9809 feet, Speed: 253 knots, Fuel: 374 gallons
Altitude: 9843 feet, Speed: 246 knots, Fuel: 369 gallons
Altitude: 9866 feet, Speed: 247 knots, Fuel: 367 gallons
Altitude: 9962 feet, Speed: 237 knots, Fuel: 365 gallons
Altitude: 10038 feet, Speed: 238 knots, Fuel: 360 gallons
Flight reached 10,000 feet. Mission accomplished!
T or F
- A simulation will always have the same result. T or F
- A simulation investigates a phenomenom without real-world constraints of time, money, or safety. T or F
- A simulation has results which are more accurate than an experiment, T or F
- A simulation can model real-worl events that are not practical for experiments
First finish Popcorn Hack #3. Expand the simulation to involve your own money.
starting money: $100
(Dice Roll <= 3) → lose $70
( 6> Dice Roll >3) → lose $40
( 9> Dice Roll >=6) → win $20
( Dice Roll>= 9 + Session Win) → win $50
Jackpot → win $100
# Code Code Code
import random
def play_popcorn_game(starting_money):
balance = starting_money
session_win = 0
while balance > 0:
dice_roll = random.randint(1, 6)
if dice_roll <= 3:
balance -= 70
elif dice_roll <= 5:
balance -= 40
elif dice_roll <= 8:
balance += 20
elif dice_roll >= 9 and session_win == 0:
balance += 50
session_win = 1
elif dice_roll == 6:
balance += 100
print(f"Roll: {dice_roll}, Balance: ${balance}")
print("Game over!")
starting_money = 100
play_popcorn_game(starting_money)
Given initial parameters for a car simulation, including its initial speed, acceleration rate, deceleration rate, maximum speed, and initial distance, write a program to simulate the car’s journey and determine the final speed, distance covered, and time taken before it either covers 1000 meters or slows down to below 5 m/s?
def car_simulation(initial_speed, acceleration_rate, deceleration_rate, max_speed, initial_distance):
time = 0
speed = initial_speed
distance_covered = initial_distance
target_distance = 1000
min_speed = 5
while distance_covered < target_distance and speed >= min_speed:
time += 1
if speed < max_speed:
speed += acceleration_rate
elif speed >= max_speed:
speed = max_speed
distance_covered += speed
if distance_covered >= target_distance:
break
elif speed > min_speed:
speed -= deceleration_rate
return speed, distance_covered, time
initial_speed = 10
acceleration_rate = 2
deceleration_rate = 1
max_speed = 20
initial_distance = 0
final_speed, final_distance, time_taken = car_simulation(initial_speed, acceleration_rate, deceleration_rate, max_speed, initial_distance)
print(f"Final Speed: {final_speed} m/s")
print(f"Distance Covered: {final_distance} meters")
print(f"Time Taken: {time_taken} seconds")
Final Speed: 20 m/s
Distance Covered: 1005 meters
Time Taken: 51 seconds
A simulation is the use of a computer software to represent the dynamic responses of one system by the behaviour of another system modeled after it. A simulation uses a mathematical descriptions, or models, of a real system in the form of a computer program.
Simulation are absractions of more complex objects or phenomena for a specific purpose
Simulations utilize varying sets of values to reflect the changings states of a phenomenon
Simulations work best when the real world experemnts are too impractical or time consuming. For example, simulating how different cars behave when they crash, would be much better than crashng actual cars in the real world, which would be expensive and dangerous.
Simulating something like a dice roll in real life would require accounting for things like: weight, flaws in design, thrust, and gravity.
- KEEP IT SIMPLE! just use a random-number generator! Ignore minor causes of variablility
#imports random module so we can use it in our code
import random
#sets variable random_number as a random number between 1 and 100
random_number = random.randint(1, 100)
#Printing out your random Number
print(random_number)
import random
def flip_coin():
return random.choice(["Heads", "Tails"])
def coin_flip_simulation(num_flips):
heads_count = 0
tails_count = 0
for _ in range(num_flips):
result = flip_coin()
if result == "Heads":
heads_count += 1
else:
tails_count += 1
return heads_count, tails_count
if __name__ == "__main__":
num_flips = 1000 #This is the number of coin flips you want to simulate
heads, tails = coin_flip_simulation(num_flips)
print("Number of Heads: "+ str(heads))
print("Number of Tails: " + str(tails))
print("Heads Probability: "+ str({heads / num_flips}))
print("Tails Probability: "+ str({tails / num_flips}))
Utilize “random” to create a basic simulation of a rolling TWO dice. Print the sum of both dice rolls. Remember to practice good syntax when naming your variables.
import random
dice1 = random.randint(0,6)
dice2 = random.randint(0,6)
print(dice1+dice2)
#Code, Code, Code
5
Simulations often utilize algorithms and equations to perform tasks because simulations don’t always have the same output
- the output of a simulation depends on the input
An algorithm is a finite sequence of instructions used to solve problems or perform computations.
- commonly used alongside functions
#Defining Function
def algorithm(input):
#Manipulating input and preparing it for the output.
output = input+2
#Return the output
return output
#Call the Function to start the algorithm
algorithm(5)
7
Simulate how long an object will fall for using an algorithm, with user-inputed variables for height dropped. Use the following formula as a reference.
# Constant, Acceleration due to gravity (m/s^2)
import math
G = 9.81
def simulation(h):
t = (2*h/G)**1/2
return t
print(simulation(100))
10.19367991845056
For loops can also be used in simulations
- They can simulate events that repeat but don’t always have the same output
# Example For Loop
#Creating For Loop to repeat 4 times
for i in range(4):
#Action that happens inside for loop
print("This is run number: " + str(i))
This is run number: 0
This is run number: 1
This is run number: 2
This is run number: 3
You are gambling addict (sigma).
Each session you roll 2 dice.
If your dice roll is greater than or equal to 9 you win the session.
If you win over 5 sessions, you win the jackpot.
Simulate your odds to predict if you will hit the jackpot (how many rounds did you win?) using a for loop and random.
import random
import math
sessions_won = 0
jackpots_hit = 0
for i in range (200):
num = random.randint(1, 6) + random.randint(1, 6)
if num >= 9:
sessions_won +=1
if sessions_won%5==0:
jackpots_hit +=1
print ("You won "+ str(sessions_won) + " sessions.")
print ("You won "+ str(jackpots_hit) + " jackpots.")
chance1 = sessions_won/200
chance2 = jackpots_hit/40
print ("Chance to win a session is " + str(sessions_won)+"/200")
print ("Chance to win a jackpot after 5 sessions is " + str(jackpots_hit)+"/40")
You won 53 sessions.
You won 32 jackpots.
Chance to win a session is 53/200
Chance to win a jackpot after 5 sessions is 32/200
Welcome to Flight Simulator! Your goal is to complete a Python program that simulates a flight We’ve set up some initial values for altitude, speed, and fuel. Your task is to update these values to make the flight more realistic.
import random
print("Welcome to Flight Simulator!")
altitude = 0
speed = 100
fuel = 1000
max_altitude = 10000
min_speed = 60
fuel_consumption_rate = 1
while altitude < max_altitude and fuel > 0:
altitude_change = random.randint(0, 100)
speed_change = random.randint(-10, 10)
fuel_change = random.randint(-5, -1)
altitude += altitude_change
speed += speed_change
if speed < min_speed:
speed = min_speed
fuel += fuel_change
if fuel < 0:
fuel = 0
print(f"Altitude: {altitude} feet, Speed: {speed} knots, Fuel: {fuel} gallons")
if altitude >= max_altitude:
print("Flight reached 10,000 feet. Mission accomplished!")
else:
print("Out of fuel. Flight mission failed.")
Welcome to Flight Simulator!
Altitude: 6 feet, Speed: 94 knots, Fuel: 996 gallons
Altitude: 100 feet, Speed: 104 knots, Fuel: 991 gallons
Altitude: 139 feet, Speed: 100 knots, Fuel: 987 gallons
Altitude: 219 feet, Speed: 102 knots, Fuel: 982 gallons
Altitude: 258 feet, Speed: 98 knots, Fuel: 981 gallons
Altitude: 357 feet, Speed: 100 knots, Fuel: 977 gallons
Altitude: 421 feet, Speed: 105 knots, Fuel: 976 gallons
Altitude: 516 feet, Speed: 106 knots, Fuel: 974 gallons
Altitude: 600 feet, Speed: 109 knots, Fuel: 969 gallons
Altitude: 655 feet, Speed: 102 knots, Fuel: 965 gallons
Altitude: 750 feet, Speed: 99 knots, Fuel: 963 gallons
Altitude: 849 feet, Speed: 89 knots, Fuel: 959 gallons
Altitude: 913 feet, Speed: 87 knots, Fuel: 954 gallons
Altitude: 994 feet, Speed: 89 knots, Fuel: 952 gallons
Altitude: 1037 feet, Speed: 88 knots, Fuel: 947 gallons
Altitude: 1096 feet, Speed: 82 knots, Fuel: 944 gallons
Altitude: 1125 feet, Speed: 78 knots, Fuel: 943 gallons
Altitude: 1151 feet, Speed: 73 knots, Fuel: 938 gallons
Altitude: 1243 feet, Speed: 80 knots, Fuel: 937 gallons
Altitude: 1342 feet, Speed: 83 knots, Fuel: 934 gallons
Altitude: 1401 feet, Speed: 77 knots, Fuel: 933 gallons
Altitude: 1423 feet, Speed: 77 knots, Fuel: 929 gallons
Altitude: 1459 feet, Speed: 70 knots, Fuel: 927 gallons
Altitude: 1469 feet, Speed: 67 knots, Fuel: 924 gallons
Altitude: 1512 feet, Speed: 62 knots, Fuel: 922 gallons
Altitude: 1577 feet, Speed: 65 knots, Fuel: 917 gallons
Altitude: 1649 feet, Speed: 60 knots, Fuel: 915 gallons
Altitude: 1726 feet, Speed: 60 knots, Fuel: 911 gallons
Altitude: 1769 feet, Speed: 65 knots, Fuel: 909 gallons
Altitude: 1805 feet, Speed: 70 knots, Fuel: 906 gallons
Altitude: 1845 feet, Speed: 73 knots, Fuel: 901 gallons
Altitude: 1925 feet, Speed: 70 knots, Fuel: 896 gallons
Altitude: 2000 feet, Speed: 67 knots, Fuel: 891 gallons
Altitude: 2031 feet, Speed: 65 knots, Fuel: 888 gallons
Altitude: 2040 feet, Speed: 64 knots, Fuel: 887 gallons
Altitude: 2087 feet, Speed: 73 knots, Fuel: 882 gallons
Altitude: 2128 feet, Speed: 81 knots, Fuel: 877 gallons
Altitude: 2160 feet, Speed: 88 knots, Fuel: 874 gallons
Altitude: 2187 feet, Speed: 92 knots, Fuel: 869 gallons
Altitude: 2229 feet, Speed: 99 knots, Fuel: 864 gallons
Altitude: 2260 feet, Speed: 97 knots, Fuel: 859 gallons
Altitude: 2271 feet, Speed: 89 knots, Fuel: 856 gallons
Altitude: 2306 feet, Speed: 95 knots, Fuel: 851 gallons
Altitude: 2317 feet, Speed: 104 knots, Fuel: 847 gallons
Altitude: 2333 feet, Speed: 97 knots, Fuel: 843 gallons
Altitude: 2426 feet, Speed: 88 knots, Fuel: 840 gallons
Altitude: 2516 feet, Speed: 97 knots, Fuel: 837 gallons
Altitude: 2571 feet, Speed: 90 knots, Fuel: 833 gallons
Altitude: 2654 feet, Speed: 100 knots, Fuel: 832 gallons
Altitude: 2655 feet, Speed: 90 knots, Fuel: 831 gallons
Altitude: 2749 feet, Speed: 98 knots, Fuel: 828 gallons
Altitude: 2781 feet, Speed: 93 knots, Fuel: 827 gallons
Altitude: 2802 feet, Speed: 94 knots, Fuel: 825 gallons
Altitude: 2820 feet, Speed: 84 knots, Fuel: 824 gallons
Altitude: 2821 feet, Speed: 87 knots, Fuel: 822 gallons
Altitude: 2823 feet, Speed: 89 knots, Fuel: 820 gallons
Altitude: 2916 feet, Speed: 98 knots, Fuel: 816 gallons
Altitude: 3016 feet, Speed: 103 knots, Fuel: 815 gallons
Altitude: 3057 feet, Speed: 95 knots, Fuel: 812 gallons
Altitude: 3081 feet, Speed: 90 knots, Fuel: 807 gallons
Altitude: 3153 feet, Speed: 99 knots, Fuel: 804 gallons
Altitude: 3242 feet, Speed: 91 knots, Fuel: 801 gallons
Altitude: 3338 feet, Speed: 93 knots, Fuel: 797 gallons
Altitude: 3379 feet, Speed: 96 knots, Fuel: 796 gallons
Altitude: 3395 feet, Speed: 100 knots, Fuel: 795 gallons
Altitude: 3422 feet, Speed: 110 knots, Fuel: 793 gallons
Altitude: 3511 feet, Speed: 118 knots, Fuel: 791 gallons
Altitude: 3596 feet, Speed: 118 knots, Fuel: 788 gallons
Altitude: 3643 feet, Speed: 123 knots, Fuel: 783 gallons
Altitude: 3673 feet, Speed: 121 knots, Fuel: 782 gallons
Altitude: 3723 feet, Speed: 131 knots, Fuel: 780 gallons
Altitude: 3728 feet, Speed: 139 knots, Fuel: 778 gallons
Altitude: 3740 feet, Speed: 148 knots, Fuel: 774 gallons
Altitude: 3791 feet, Speed: 144 knots, Fuel: 771 gallons
Altitude: 3867 feet, Speed: 139 knots, Fuel: 766 gallons
Altitude: 3873 feet, Speed: 148 knots, Fuel: 765 gallons
Altitude: 3895 feet, Speed: 150 knots, Fuel: 764 gallons
Altitude: 3918 feet, Speed: 150 knots, Fuel: 761 gallons
Altitude: 3954 feet, Speed: 142 knots, Fuel: 760 gallons
Altitude: 4048 feet, Speed: 152 knots, Fuel: 756 gallons
Altitude: 4106 feet, Speed: 155 knots, Fuel: 752 gallons
Altitude: 4201 feet, Speed: 153 knots, Fuel: 749 gallons
Altitude: 4254 feet, Speed: 146 knots, Fuel: 744 gallons
Altitude: 4302 feet, Speed: 140 knots, Fuel: 742 gallons
Altitude: 4367 feet, Speed: 134 knots, Fuel: 739 gallons
Altitude: 4451 feet, Speed: 134 knots, Fuel: 736 gallons
Altitude: 4484 feet, Speed: 134 knots, Fuel: 733 gallons
Altitude: 4492 feet, Speed: 131 knots, Fuel: 730 gallons
Altitude: 4552 feet, Speed: 128 knots, Fuel: 726 gallons
Altitude: 4622 feet, Speed: 138 knots, Fuel: 724 gallons
Altitude: 4636 feet, Speed: 143 knots, Fuel: 723 gallons
Altitude: 4640 feet, Speed: 152 knots, Fuel: 721 gallons
Altitude: 4644 feet, Speed: 145 knots, Fuel: 719 gallons
Altitude: 4728 feet, Speed: 150 knots, Fuel: 716 gallons
Altitude: 4755 feet, Speed: 143 knots, Fuel: 714 gallons
Altitude: 4803 feet, Speed: 141 knots, Fuel: 709 gallons
Altitude: 4816 feet, Speed: 147 knots, Fuel: 704 gallons
Altitude: 4859 feet, Speed: 149 knots, Fuel: 699 gallons
Altitude: 4916 feet, Speed: 141 knots, Fuel: 694 gallons
Altitude: 5009 feet, Speed: 149 knots, Fuel: 693 gallons
Altitude: 5053 feet, Speed: 150 knots, Fuel: 692 gallons
Altitude: 5070 feet, Speed: 152 knots, Fuel: 691 gallons
Altitude: 5081 feet, Speed: 159 knots, Fuel: 690 gallons
Altitude: 5117 feet, Speed: 157 knots, Fuel: 687 gallons
Altitude: 5157 feet, Speed: 151 knots, Fuel: 683 gallons
Altitude: 5200 feet, Speed: 159 knots, Fuel: 682 gallons
Altitude: 5247 feet, Speed: 151 knots, Fuel: 681 gallons
Altitude: 5312 feet, Speed: 146 knots, Fuel: 676 gallons
Altitude: 5316 feet, Speed: 141 knots, Fuel: 673 gallons
Altitude: 5347 feet, Speed: 135 knots, Fuel: 670 gallons
Altitude: 5437 feet, Speed: 130 knots, Fuel: 669 gallons
Altitude: 5496 feet, Speed: 126 knots, Fuel: 666 gallons
Altitude: 5505 feet, Speed: 126 knots, Fuel: 664 gallons
Altitude: 5546 feet, Speed: 123 knots, Fuel: 661 gallons
Altitude: 5643 feet, Speed: 133 knots, Fuel: 660 gallons
Altitude: 5670 feet, Speed: 127 knots, Fuel: 658 gallons
Altitude: 5732 feet, Speed: 118 knots, Fuel: 654 gallons
Altitude: 5820 feet, Speed: 109 knots, Fuel: 650 gallons
Altitude: 5877 feet, Speed: 101 knots, Fuel: 648 gallons
Altitude: 5931 feet, Speed: 110 knots, Fuel: 643 gallons
Altitude: 5959 feet, Speed: 115 knots, Fuel: 638 gallons
Altitude: 6049 feet, Speed: 119 knots, Fuel: 637 gallons
Altitude: 6076 feet, Speed: 129 knots, Fuel: 632 gallons
Altitude: 6148 feet, Speed: 125 knots, Fuel: 629 gallons
Altitude: 6208 feet, Speed: 115 knots, Fuel: 625 gallons
Altitude: 6252 feet, Speed: 106 knots, Fuel: 622 gallons
Altitude: 6298 feet, Speed: 115 knots, Fuel: 618 gallons
Altitude: 6355 feet, Speed: 125 knots, Fuel: 617 gallons
Altitude: 6444 feet, Speed: 123 knots, Fuel: 613 gallons
Altitude: 6487 feet, Speed: 116 knots, Fuel: 610 gallons
Altitude: 6497 feet, Speed: 106 knots, Fuel: 605 gallons
Altitude: 6500 feet, Speed: 97 knots, Fuel: 604 gallons
Altitude: 6502 feet, Speed: 102 knots, Fuel: 600 gallons
Altitude: 6555 feet, Speed: 100 knots, Fuel: 597 gallons
Altitude: 6579 feet, Speed: 109 knots, Fuel: 592 gallons
Altitude: 6667 feet, Speed: 112 knots, Fuel: 588 gallons
Altitude: 6683 feet, Speed: 109 knots, Fuel: 587 gallons
Altitude: 6752 feet, Speed: 116 knots, Fuel: 586 gallons
Altitude: 6852 feet, Speed: 123 knots, Fuel: 584 gallons
Altitude: 6856 feet, Speed: 122 knots, Fuel: 583 gallons
Altitude: 6928 feet, Speed: 131 knots, Fuel: 582 gallons
Altitude: 6931 feet, Speed: 127 knots, Fuel: 578 gallons
Altitude: 6947 feet, Speed: 125 knots, Fuel: 576 gallons
Altitude: 7008 feet, Speed: 134 knots, Fuel: 575 gallons
Altitude: 7056 feet, Speed: 144 knots, Fuel: 570 gallons
Altitude: 7140 feet, Speed: 150 knots, Fuel: 569 gallons
Altitude: 7189 feet, Speed: 147 knots, Fuel: 566 gallons
Altitude: 7221 feet, Speed: 152 knots, Fuel: 561 gallons
Altitude: 7247 feet, Speed: 144 knots, Fuel: 558 gallons
Altitude: 7293 feet, Speed: 149 knots, Fuel: 554 gallons
Altitude: 7380 feet, Speed: 142 knots, Fuel: 551 gallons
Altitude: 7459 feet, Speed: 146 knots, Fuel: 549 gallons
Altitude: 7462 feet, Speed: 156 knots, Fuel: 544 gallons
Altitude: 7520 feet, Speed: 152 knots, Fuel: 543 gallons
Altitude: 7594 feet, Speed: 158 knots, Fuel: 542 gallons
Altitude: 7659 feet, Speed: 148 knots, Fuel: 540 gallons
Altitude: 7731 feet, Speed: 156 knots, Fuel: 535 gallons
Altitude: 7732 feet, Speed: 164 knots, Fuel: 533 gallons
Altitude: 7755 feet, Speed: 168 knots, Fuel: 531 gallons
Altitude: 7771 feet, Speed: 175 knots, Fuel: 527 gallons
Altitude: 7788 feet, Speed: 175 knots, Fuel: 522 gallons
Altitude: 7815 feet, Speed: 176 knots, Fuel: 521 gallons
Altitude: 7819 feet, Speed: 180 knots, Fuel: 517 gallons
Altitude: 7823 feet, Speed: 181 knots, Fuel: 514 gallons
Altitude: 7850 feet, Speed: 190 knots, Fuel: 509 gallons
Altitude: 7872 feet, Speed: 188 knots, Fuel: 506 gallons
Altitude: 7941 feet, Speed: 195 knots, Fuel: 501 gallons
Altitude: 7981 feet, Speed: 200 knots, Fuel: 498 gallons
Altitude: 8042 feet, Speed: 210 knots, Fuel: 494 gallons
Altitude: 8046 feet, Speed: 219 knots, Fuel: 489 gallons
Altitude: 8114 feet, Speed: 229 knots, Fuel: 484 gallons
Altitude: 8118 feet, Speed: 239 knots, Fuel: 483 gallons
Altitude: 8200 feet, Speed: 247 knots, Fuel: 478 gallons
Altitude: 8238 feet, Speed: 246 knots, Fuel: 473 gallons
Altitude: 8241 feet, Speed: 255 knots, Fuel: 471 gallons
Altitude: 8330 feet, Speed: 246 knots, Fuel: 470 gallons
Altitude: 8388 feet, Speed: 244 knots, Fuel: 466 gallons
Altitude: 8450 feet, Speed: 241 knots, Fuel: 465 gallons
Altitude: 8464 feet, Speed: 235 knots, Fuel: 461 gallons
Altitude: 8472 feet, Speed: 237 knots, Fuel: 460 gallons
Altitude: 8507 feet, Speed: 246 knots, Fuel: 456 gallons
Altitude: 8532 feet, Speed: 244 knots, Fuel: 452 gallons
Altitude: 8580 feet, Speed: 251 knots, Fuel: 447 gallons
Altitude: 8636 feet, Speed: 241 knots, Fuel: 443 gallons
Altitude: 8698 feet, Speed: 245 knots, Fuel: 439 gallons
Altitude: 8791 feet, Speed: 245 knots, Fuel: 435 gallons
Altitude: 8826 feet, Speed: 252 knots, Fuel: 431 gallons
Altitude: 8860 feet, Speed: 246 knots, Fuel: 430 gallons
Altitude: 8886 feet, Speed: 252 knots, Fuel: 427 gallons
Altitude: 8969 feet, Speed: 251 knots, Fuel: 424 gallons
Altitude: 9011 feet, Speed: 255 knots, Fuel: 420 gallons
Altitude: 9018 feet, Speed: 264 knots, Fuel: 416 gallons
Altitude: 9036 feet, Speed: 257 knots, Fuel: 412 gallons
Altitude: 9056 feet, Speed: 258 knots, Fuel: 408 gallons
Altitude: 9145 feet, Speed: 260 knots, Fuel: 404 gallons
Altitude: 9203 feet, Speed: 261 knots, Fuel: 402 gallons
Altitude: 9251 feet, Speed: 269 knots, Fuel: 397 gallons
Altitude: 9337 feet, Speed: 264 knots, Fuel: 392 gallons
Altitude: 9353 feet, Speed: 264 knots, Fuel: 390 gallons
Altitude: 9444 feet, Speed: 258 knots, Fuel: 389 gallons
Altitude: 9494 feet, Speed: 252 knots, Fuel: 387 gallons
Altitude: 9556 feet, Speed: 253 knots, Fuel: 384 gallons
Altitude: 9644 feet, Speed: 259 knots, Fuel: 381 gallons
Altitude: 9717 feet, Speed: 260 knots, Fuel: 377 gallons
Altitude: 9809 feet, Speed: 253 knots, Fuel: 374 gallons
Altitude: 9843 feet, Speed: 246 knots, Fuel: 369 gallons
Altitude: 9866 feet, Speed: 247 knots, Fuel: 367 gallons
Altitude: 9962 feet, Speed: 237 knots, Fuel: 365 gallons
Altitude: 10038 feet, Speed: 238 knots, Fuel: 360 gallons
Flight reached 10,000 feet. Mission accomplished!
T or F
- A simulation will always have the same result. T or F
- A simulation investigates a phenomenom without real-world constraints of time, money, or safety. T or F
- A simulation has results which are more accurate than an experiment, T or F
- A simulation can model real-worl events that are not practical for experiments
First finish Popcorn Hack #3. Expand the simulation to involve your own money.
starting money: $100
(Dice Roll <= 3) → lose $70
( 6> Dice Roll >3) → lose $40
( 9> Dice Roll >=6) → win $20
( Dice Roll>= 9 + Session Win) → win $50
Jackpot → win $100
# Code Code Code
import random
def play_popcorn_game(starting_money):
balance = starting_money
session_win = 0
while balance > 0:
dice_roll = random.randint(1, 6)
if dice_roll <= 3:
balance -= 70
elif dice_roll <= 5:
balance -= 40
elif dice_roll <= 8:
balance += 20
elif dice_roll >= 9 and session_win == 0:
balance += 50
session_win = 1
elif dice_roll == 6:
balance += 100
print(f"Roll: {dice_roll}, Balance: ${balance}")
print("Game over!")
starting_money = 100
play_popcorn_game(starting_money)
Given initial parameters for a car simulation, including its initial speed, acceleration rate, deceleration rate, maximum speed, and initial distance, write a program to simulate the car’s journey and determine the final speed, distance covered, and time taken before it either covers 1000 meters or slows down to below 5 m/s?
def car_simulation(initial_speed, acceleration_rate, deceleration_rate, max_speed, initial_distance):
time = 0
speed = initial_speed
distance_covered = initial_distance
target_distance = 1000
min_speed = 5
while distance_covered < target_distance and speed >= min_speed:
time += 1
if speed < max_speed:
speed += acceleration_rate
elif speed >= max_speed:
speed = max_speed
distance_covered += speed
if distance_covered >= target_distance:
break
elif speed > min_speed:
speed -= deceleration_rate
return speed, distance_covered, time
initial_speed = 10
acceleration_rate = 2
deceleration_rate = 1
max_speed = 20
initial_distance = 0
final_speed, final_distance, time_taken = car_simulation(initial_speed, acceleration_rate, deceleration_rate, max_speed, initial_distance)
print(f"Final Speed: {final_speed} m/s")
print(f"Distance Covered: {final_distance} meters")
print(f"Time Taken: {time_taken} seconds")
Final Speed: 20 m/s
Distance Covered: 1005 meters
Time Taken: 51 seconds