Vocab

  • Simulation: an imitation of a situation or process
  • Ex)
    • testing safety of a car
    • games
    • testing the efficiency of a parking lot
    • testing a new train route
  • Experiment: procedure undertaken to make a discovery, test a hypothesis, or demonstrate a known fact

Example

  • For example, suggesting to build snowman can be made by simulation.
import time
Elsa = False
print("Do you want to build the snowman?:")
time.sleep(2)
if Elsa == True:
    b = 'Yes'
elif Elsa == False:
    b = "Go Away"
print(b)
Do you want to build the snowman?:
Go Away

Experiment

anna

goAway

Notes

  • Advantage of simulation
    • Can be safer
    • More cost-effective
    • More efficient
    • More data in less time
  • Disadvantage of simulation
    • Not as accurate as experiments
    • outside factors not included (ex: in rolling dice simulation gravity and air resistance)
  • When do you not use a simulation?
    • when a situation already has set results/data (won't change)
    • examples: a score in a game, most purchased food, average yearly wage

Hack 1

writing example of simulation

The simulation I am trying to write is Russian Roulette. After putting only one bullet into a six-shot revolver, turning the cylinder, and turning each other, they put the gun to their head and pull the trigger. If there are 1, 2, 3, or 6 factors of 6 playing the game, the odds of dying are the same. With any other number, the number of 6 attempts cannot be divided evenly, so there will inevitably be a person with a high probability of dying. If the person in front succeeds, the probability that the person behind will die increases, but since the probability of the person in front dying first and the probability of not dying because I shoot are combined, the probability of dying at the start of the game is fair. If no one has died by the 5th, the 6th chamber will of course contain bullets. There is also a rule to rotate the cylinder for each shot. In this case, regardless of the number of people, there is an equal 1/6 chance of death each turn, but if you can run away without shooting when all opponents are dead, the later you shoot, the more advantageous it is.

  • Guided question
    • What make it simulation? The random value of the bullet can make simulation. If the bullet was in the first round, the first person will die, but if the bullet is in the last round, the last person will die.
    • What are it’s advantages and disadvantages? Running this simulation gives you an idea of ​​what might happen. However, the disadvantages of this simulation is that it's not useful in your life. Even if the number of cases is known, there is no way to use it, so it is very useless in real life.
    • In your opinion, would an experiment be better in this situation? I think it is better to be an experiment, because we have to gather multiple people for doing this game.

Russian

import random
def enter():
    while True:
        n = input("shoot")
        if n == "":
            break
if __name__ == "__main__":
    bullet = ["alive", "alive", "alive", "alive", "alive", "death"]
    i = 5
    while True:
        idx_bullet = random.randint(0, i)
        enter()
        a = bullet.pop(idx_bullet)
        print(a)
        if a == "death":
            break
        i = i - 1
alive
death

Hack 2

Quiz

questions_number = 6
answers_correct = 0
questions = [
    "True or False: Simulations will always have the same result. \n A: True, \n B: False",
    "True or False: A simulation has results that are more accurate than an experiment \n A: True, \n B: False",
    "True or False: A simulation can model real world events that are not practical for experiments \n A: True, \n B: False",
    "Which one of these is FALSE regarding simulations \n A: Reduces Costs, \n B: Is safer than real life experiments, \n C: More Efficient, \n D: More accurate than real life experiments",
    "Which of the following scenarios would be the LEAST beneficial to have as a simulation \n A: A retail company wants to identify the item which sold the most on their website, \n B: A restaurant wants to determine if the use of robots will increase efficiency, \n C: An insurance company wants to study the impact of rain on car accidents, \n D: A sports car company wants to study design changes to their new bike design ",
    "Which of the following is better to do as a simulation than as a calculation \n A: Keeping score at a basketball game, \n B: Keeping track of how many games a person has won, \n C: Determining the average grade for a group of tests, \n D: Studying the impact of carbon emissions on the environment"
]
question_answers = [
    "B",
    "B",
    "A",
    "D",
    "A",
    "D"
]

print("Welcome to the Simulations Quiz!")

def ask_question (question, answer):
    print("\n", question)
    user_answer = input(question)
    print("You said: ", user_answer)

    if user_answer == answer:
        print("Correct!")
        global answers_correct
        answers_correct = answers_correct + 1
    else:
        print("You are incorrect")
    
for num in range(questions_number):
    ask_question(questions[num], question_answers[num])

print("You scored: ", answers_correct, "/6")
Welcome to the Simulations Quiz!

 True or False: Simulations will always have the same result. 
 A: True, 
 B: False
You said:  B
Correct!

 True or False: A simulation has results that are more accurate than an experiment 
 A: True, 
 B: False
You said:  B
Correct!

 True or False: A simulation can model real world events that are not practical for experiments 
 A: True, 
 B: False
You said:  A
Correct!

 Which one of these is FALSE regarding simulations 
 A: Reduces Costs, 
 B: Is safer than real life experiments, 
 C: More Efficient, 
 D: More accurate than real life experiments
You said:  D
Correct!

 Which of the following scenarios would be the LEAST beneficial to have as a simulation 
 A: A retail company wants to identify the item which sold the most on their website, 
 B: A restaurant wants to determine if the use of robots will increase efficiency, 
 C: An insurance company wants to study the impact of rain on car accidents, 
 D: A sports car company wants to study design changes to their new bike design 
You said:  A
Correct!

 Which of the following is better to do as a simulation than as a calculation 
 A: Keeping score at a basketball game, 
 B: Keeping track of how many games a person has won, 
 C: Determining the average grade for a group of tests, 
 D: Studying the impact of carbon emissions on the environment
You said:  D
Correct!
You scored:  6 /6

Hack 3

Rolling Dices

  • pick a random number between 1 and 6. It is because the dice has six parts. If it is 14 - sided dice, you have to pick number in 1 to 14.
  • After picking the two numbers for a and b, compare a and b. There will be three results: a is bigger than b, a is equal to b, and a is smaller than b.
  • First, if a is bigger than b, print the word about the winning of A. Second, if a is equal to b, there will no winner in this game, so it has to be printed "draw". Third, if a is smaller than b, the b becomes a winner. Therefore, the end result will be "B won".
import random

start = input('Press Enter to Roll the dices : ')

a = random.randrange(1,6)
b = random.randrange(1,6)

print(f"A's dice is {a}. ")
print(f"B's dice is {b}. ")

if a > b:
    print('A won')
    
elif a == b:
    print('draw')
    
else:
    print('B won')
A's dice is 2. 
B's dice is 4. 
B won

Guided Question

  • What makes it a simulation?
    • We can say this is a simulation because we're not rolling the dices in real world and just writing the codes in our computer.
  • what are it’s advantages and disadvantages?
    • Advantages we can get is that we don't have to prepare some dices to roll. We can just make sure to choose the random value from the computer. Disadvantages is that it is more easy to simulate in real world, but in computer world, we have to write the letters instead of rolling dices in the real world. It can be hard for someone who has slow typing skill.
  • In your opinion, would an experiment be better in this situation?
    • I think rolling dice in real world is much better because it caused outside factors.

Hack 4

Making game of rolling dice

  • This is the code of game
    • At first, two dices are given
    • three people and one dealer are playing this game.
    • Game starts with the order of User 1 to User2 and to User 3
    • Player can only bet maximum 10000 dollars
    • Players have to fight with the dealer and person who got bigger numbers of two dices will win
    • when it happens to draw, the user will win
    • If dealer wins the users lose their money, but if users win, they will get money.
    • The conditionals to make game over is that two users have to lose all of their money.
    • The only person who left in the end will be the winner.
from random import randint

SEEDMAX = 100000
BETMAX = 10000


def seed(name):
    s = int(input(f"{name} seed(1~{SEEDMAX}): "))
    return s if 1<=s<=SEEDMAX else seed(name)


def bet(usr):
    lmt = min(usr['money'], BETMAX)
    b = int(input(f"{usr['name']} bet(1~{lmt}): "))
    return b if 1<=b<=lmt else bet(usr)


def roll():
    return randint(1, 6), randint(1, 6)


def dbl(d):
    return d[0] == d[1]


def score(d):
    return d[0] + d[1] + dbl(d) * 10


def play(usr, bet):
    rd, ru = roll(), roll()
    if score(ru) >= score(rd):
        winner, loser = (usr['name'], ru), ('dealer', rd)
    else:
        winner, loser = ('dealer', rd), (usr['name'], ru)

    return calc(usr, bet, winner, loser)


def calc(usr, bet, winner, loser):
    doubled = dbl(winner[1]) and not dbl(loser[1])
    stake =  bet * (1 + doubled)
    if usr['name'] in winner:
        usr['money'] += stake
    else:
        usr['money'] -= stake

    print("{}{} win{}, {}{} lose, {}'s money={}".format(*winner, '(x2)'*doubled, *loser, *usr.values()))
    return usr['money'] > 0


usrs = [{'name':n, 'money':seed(n)} for n in ('User1', 'User2', 'User3')]
while len(usrs) > 1:
    usr = usrs.pop(0)
    usrs += [usr] if play(usr, bet(usr)) else []
    print()

last_usr = max(usrs, key=lambda u: u['money'])
print('Finally {} win, money={}'.format(*last_usr.values()))
dealer(4, 5) win, User1(6, 1) lose, User1's money=5000

dealer(2, 5) win, User2(3, 1) lose, User2's money=6000

User3(3, 6) win, dealer(2, 4) lose, User3's money=13000

User1(3, 3) win(x2), dealer(3, 4) lose, User1's money=10000

User2(3, 2) win, dealer(3, 1) lose, User2's money=9000

dealer(6, 2) win, User3(2, 3) lose, User3's money=9000

User1(1, 4) win, dealer(1, 4) lose, User1's money=20000

User2(1, 1) win(x2), dealer(5, 6) lose, User2's money=27000

dealer(3, 5) win, User3(1, 3) lose, User3's money=0

User1(2, 2) win(x2), dealer(5, 3) lose, User1's money=40000

User2(6, 1) win, dealer(5, 1) lose, User2's money=37000

dealer(3, 6) win, User1(4, 2) lose, User1's money=30000

User2(2, 4) win, dealer(1, 2) lose, User2's money=47000

dealer(1, 1) win(x2), User1(4, 1) lose, User1's money=10000

dealer(2, 6) win, User2(1, 2) lose, User2's money=37000

dealer(1, 1) win(x2), User1(4, 2) lose, User1's money=-10000

Finally User2 win, money=37000

Guided Question

  • What makes it a simulation?
    • This is the simulation of Gambling. People bet their money. Sometimes they lose it, but sometimes they earn more money.
  • What are it’s advantages and disadvantages?
    • The advantages of this simulation is that we can enjoy gambling in our houses. It can make us feel excited when you're boring. The disadvantages of this simulation is that it's not perfect. It's just a simple game, so it will be not useful soon. And the excitement of gambling comes from the real money. Thrills and hope in the gambling makes people fun.
  • In your opinion, would an experiment be better in this situation?
    • I think it is not good to be in experiment. We don't be happy when the fate money increased. We want to get money by winning in the gambling.

Extra things

  • I did two of it. ### First one is Bingo #### Description
  • This is the code of bingo game.
  • It is a same rule with the normal bingo.
  • we can choose the dimension.
def board(bingo, dimension):
    for i in range(dimension):
        print(' _', end = '')
    for i in range(dimension):
        print()
        print('|', end = '')
        for j in range(dimension):
            print(bingo[i][j] + '|', end = '')
    print()


while True:
    dimension = int(input("Please input the size of the game board(more than 2): "))
    if dimension <= 2 :
        print('[Error] try again')
    else:
        break

bingo = [['_']*dimension for i in range(dimension)]
board(bingo, dimension)

turn = 1
play_count = 0
while True:
    print('<Play no.{}>'.format(play_count+1))
    if turn == 1:
        print('Currently player: 1')
        row_1 = int(input('Which row?(start with 1)'))
        column_1 = int(input('Which column?(start with 1)'))

        if bingo[row_1-1][column_1-1] != '_':
            print('Space is not empty. Try again')
            turn = 1
            continue
        else:
            bingo[row_1-1][column_1-1] = 'O'
            board(bingo,dimension)
            turn = 2

    elif turn == 2:
        print('Currently player: 2')
        row_2 = int(input('Which row?(start with 1) '))
        column_2 = int(input('Which column?(start with 1) '))

        if bingo[row_2-1][column_2-1] != '_':
            print('Space is not empty. Try again')
            turn = 2
            continue
        else:
            bingo[row_2-1][column_2-1] = 'X'
            board(bingo,dimension)
            turn = 1

    check_diag = []
    check_reverse = []
    check_row = []
    check_column =[]
            
    for i in range(dimension):
        check_diag.append(bingo[i][i])
        check_reverse.append(bingo[dimension-i-1][i])
        

        for j in range(dimension):
            check_row.append(bingo[i][j])
            check_column.append(bingo[j][i])
        
        if set(check_row) == {'O'}:
            print('Player 1 wins!')
            turn = 0
        elif set(check_row) == {'X'}:
            print('Player 2 wins!')
            turn = 0

        check_row = []

        if set(check_column) == {'O'}:
            print('Player 1 wins!')
            turn = 0
        elif set(check_column) == {'X'}:
            print('Player 2 wins!')
            turn = 0
        check_column = []

    check_diag = set(check_diag)
    check_reverse = set(check_reverse)

    if check_diag == {'O'} or check_reverse == {'O'}:
        print('Player 1 wins!')
        turn = 0
        
    elif check_diag == {'X'} or check_reverse == {'X'}:
        print('Player 2 wins!')
        turn = 0

    play_count += 1


    if turn == 0 or play_count == dimension**2:
        print('Finish')
        break
 _ _ _
|_|_|_|
|_|_|_|
|_|_|_|
<Play no.1>
Currently player: 1
 _ _ _
|O|_|_|
|_|_|_|
|_|_|_|
<Play no.2>
Currently player: 2
 _ _ _
|O|_|_|
|_|X|_|
|_|_|_|
<Play no.3>
Currently player: 1
 _ _ _
|O|_|O|
|_|X|_|
|_|_|_|
<Play no.4>
Currently player: 2
 _ _ _
|O|_|O|
|_|X|_|
|_|_|X|
<Play no.5>
Currently player: 1
 _ _ _
|O|O|O|
|_|X|_|
|_|_|X|
Player 1 wins!
Finish

Guided Question

  • What makes it a simulation?
    • It might be seemed to simulation because we are not doing Bingo on paper instead of writing many codes.
  • What are it’s advantages and disadvantages?
    • Bingo can be played without paper. And disadvantages is that we can only play bingo with two people.
  • In your opinion, would an experiment be better in this situation?
    • I think it is not a good way to be experiment. It will be funnier when we play bingo in the real life and 3D.

Second one is Rock, Scissor, Paper

Description

  • same rule with rock scissor paper
  • get the value by input
  • the value must be rock or scissor or paper
  • compare with the computer
  • if player wins, print "player win"
  • if computer wins, print "computer win"
  • if they are small, print "draw"
import random

sel = ['scissor', 'rock', 'paper']
result = {0: 'win.', 1: 'lose.', 2: 'draw.'}

def checkWin(user, com):

    if not user in sel:
       print('you wrote wrong. type it again')
       return False

    print(f'player 1 ( {user} vs {com} ) com')
    if user == com:
        state = 2
    elif user == 'scissor' and com == 'rock':
        state = 1
    elif user == 'rock' and com == 'paper':
        state = 1
    elif user == 'paper' and com == 'scissor':
        state = 1
    else:
        state = 0
    print(result[state])
    return True


print('\n-------------------------------------------')
while True:
    user = input("rock, scissor, paper : ")
    com = sel[random.randint(0, 2)]
    if checkWin(user, com):
        break
print('-------------------------------------------\n')
-------------------------------------------
player 1 ( rock vs scissor ) com
win.
-------------------------------------------

Guided question

  • What makes it a simulation?
    • Doing rock scissor paper with not using hands can be the simulation.
  • What are it’s advantages and disadvantages?
    • Advantage is that we can do it alone. Sometimes we are boring with something, playing these game will help you. It is not efficient because we can only do with computer
  • In your opinion, would an experiment be better in this situation?
    • It will be beneficial when we do this with your friends, because rock scissor paper is a game which is played with many people.

Please Give me Perfect Score