Lesson 3.16 - Simulations
Notes, questions, and hacks
Notes
-
simulation: a simulation is an imitation of a situation or process
- i.e., testing cars, games, train routes, etc.
- Advantages
- safer
- cost-efficient
- time-efficient
- Disadvantages
- not as accurate
- outside factors not included
- experiment: procedure undertaken to make a discovery, test a hypothesis, or demonstrate a known fact
- do NOT use simulation when:
- data is already set and cannot change
- i.e. score, statistics
- data is already set and cannot change
- Class simulation vs code:
- Mr. Mortensen = random.randint
- if statement = if player is out
- else = safe
- while loop = repeat process until winner
Hacks
Hack #1
Rock Paper Scissors Simulation
You are Player A. You can input whether you want to shoot rock, paper or scissors. At random, either rock, paper, or scissors, will be assigned to Player B. If the two players' values are equal, then they will both receive a point. If one player gets rock and the other paper, paper will win, and that player will receive a point. This will also apply to scissors beating paper and rock beating scissors. The first player to receive 3 points wins the game, and the simulation stops.
Hack #2
Hack #3
The rolling dice simulator asks the user to choose an amount of dice to roll (1 through 6). Then, once there is an input, the simulator outputs dice rolls (a value 1 through 6), the number of times chosen by the user. For example, I chose 2 dice, and I received the values 6 and 1. This simulates the dice rolls I would roll on 2 dice in real life.
Hack #4
def parse_input(input_string):
if input_string.strip() in {"1", "2", "3","4", "5", "6"}:
return int(input_string)
else:
print("Please enter a number from 1 to 6.")
raise SystemExit(1)
import random
def roll_dice(num_dice):
roll_results = []
for _ in range(num_dice):
roll = random.randint(1, 10)
roll_results.append(roll)
return roll_results
def sumOfList(list, size):
if (size == 0):
return 0
else:
return list[size - 1] + sumOfList(list, size - 1)
num_dice_input = input("How many dice do you want to roll? [1-6] ")
num_dice = parse_input(num_dice_input)
roll_results = roll_dice(num_dice)
total = sumOfList(roll_results, len(roll_results))
print("you rolled:", roll_results)
print("sum:", total)
I modified the dice into a 10 sided dice, then I added a function that adds all the dice roll values together. Now the code outputs both the list of values rolled for the amount of dice inputted, as well as its sum.
import random
round = 1
a = 0
b = 0
for a in range(0, 3) or b in range(0, 4):
print("\nRound", round)
throwA = input("Would you like to throw rock, paper, or scissors?")
print("You:", throwA)
throws = ['rock', 'paper', 'scissors']
throwB = random.choice(throws)
print("Annika:", throwB)
if throwA == throwB:
a += 1
b += 1
print("Tie, you both selected " + throwA + ". You both receive a point.")
elif throwA == "rock":
if throwB == "scissors":
a += 1
print("Rock smashes scissors!")
else:
b += 1
print("Paper covers rock!")
elif throwA == "paper":
if throwB == "rock":
a += 1
print("Paper covers rock!")
else:
b += 1
print("Scissors cuts paper!")
elif throwA == "scissors":
if throwB == "paper":
a += 1
print("Scissors cuts paper!")
else:
b += 1
print("Rock smashes scissors!")
print("\nYour score:", a)
print("Annika's score:", b)
round +=1
print("Game over!")
I coded the simulation that I had described in Hack 1. I coded a rock paper scissors game where you are playing against a computer with randomized outputs. The first one to score 3 points wins! You score a point when your throw beats the computer. Rock wins scissors, paper wins rock, and scissors wins paper. If tied, you both score a point. This may not be authentic to a real life experiment, because the machine doesn't take in outside factors, such as the other player's strategies. They could throw late, allowing them to see you you throw before throwing themselves.