# import random library and set seed
import numpy as np
x = np.random.seed(123456)
# Step 1.
# Define a function which randomly draws a ball with an integer between 1 and 100 on it;
# then returns True if Player
# wins and returns False if House wins.
# Add a print statement to function to print random integer and test out function
# Add comment statements to function
#
# Function rball has no inputs and randomly selects a ball value between 1 and 100
# It returns true if a player wins when the ball is 52 or greater and false for all other values.
def rball() :
ballnum = np.random.randint(1,100,1)
if (ballnum >= 52) :
return True
else :
return False
#rball()
# Step 2.
# Create a function which simulates playing the game n_plays times; keep track of number of wins and losses
# Here a new function is created which returns the number of wins after n_plays
def numberofgamesplayed(n_plays) :
# Below the counter variables are initialized
numberofgplayed = 0
numberofwins = 0
numberoflosses = 0
# Here the function rball is called which randomly selects a ball number.
# When rball returns True the number of wins inceases and similarly for False the number of losses increases
# When n_plays is reached the function returns the total number of wins
for i in range (0,n_plays) :
numbervalue = rball()
if (numbervalue == True) :
numberofwins = numberofwins + 1
numberofgplayed = numberofgplayed + 1
elif (numbervalue == False) :
numberoflosses = numberoflosses + 1
numberofgplayed = numberofgplayed + 1
if (numberofgplayed == n_plays):
#print(f"For {n_plays} games there were {numberofwins} wins and {numberoflosses} losses.")
return(numberofwins)
# Here I test the function to confirm that the number of wins for a 100 games is returned
numberofgamesplayed(100)
# Here a new function which takes in the number of games and returns the total remaining after the
# games are played and the number of wins that occurs.
# It takes the number of wins from the numberofgamesplayed function and uses the fixed betting
# amounts and gambling funds.
def amtoffunds(games) :
gamblingmoney = 10000
pricepergame = 100
numofwins = numberofgamesplayed(games)
numoflosses = games - numofwins
totalspent = games*pricepergame
totalwon = numofwins*pricepergame
# Notice that the total remaining adds back the funds that have been won in previous games
totalremaining = gamblingmoney - totalspent + totalwon
#print(f"For {games} games the total spent was {totalspent}. There were {numoflosses} losses and {numofwins} wins.")
#print(f"Of the total remaining ${totalremaining}, ${totalwon} were won in the game.")
return(totalremaining, numofwins)
amtoffunds(5)
# Counter and fixed variables are initialized
numberofrepetitions = 100
sumofremaining = 0
totalnumofwins = 0
# Here 100 repetitions are run to simulate playing 5 games 100 times. It takes in the amount remaining and
# the number of wins from the amtoffunds function when the game is played 5 times. For each iteration the
# the numberofwins and amount of funds won is summed to obtain a total that is later divided to obtain
# an average dollar amoutn remaining and probability of winning.
for i in range (0,numberofrepetitions) :
currenttotal,numberofwins1 = amtoffunds(5)
sumofremaining = sumofremaining + currenttotal
totalnumofwins = totalnumofwins + numberofwins1
averageremaining = sumofremaining/numberofrepetitions
probofwin = totalnumofwins/(numberofrepetitions*5)
print(f"The average amount remaining is ${averageremaining}")
print(f"The probability of winning is {probofwin}")
expectedvalue = (100)*(probofwin) - (100)*(1-probofwin)
print(f"The expected value from our game is ${expectedvalue}")
# I increased the number of repetitions needed to get the theoretical expected value
# similar to the discrete expected value.
numberofrepetitions = 1000000
sumofremaining = 0
totalnumofwins = 0
for i in range (0,numberofrepetitions) :
currenttotal,numberofwins1 = amtoffunds(5)
sumofremaining = sumofremaining + currenttotal
totalnumofwins = totalnumofwins + numberofwins1
averageremaining = sumofremaining/numberofrepetitions
probofwin = totalnumofwins/(numberofrepetitions*5)
print(f"The average amount remaining is ${averageremaining}")
print(f"The probability of winning is {probofwin}")
expectedvalue = (100)*(probofwin) - (100)*(1-probofwin)
print(f"The expected value from our game is ${expectedvalue}")
# 1. import random and set seed
# 2. Input target sales and number of sales persons for upcoming year; low and high
# percentages of sales for generating random numbers
# 3. Generate a random sales amount for each rep and check that it is in the correct range
# Use a for loop; round answer to nearest cent
# 4. Add to your loop in # 3 to determine the commission for each rep
# (you need an if-elif-else construct to determine rate); keep a running tab of
# the sum of all commissions
# round all dollar amounts to nearest cent
#
# 5. Add a loop around your code in # 4 to do 10,000 simulations
# Only print out the average commission for all 10,000 simulations
# Don't print out all the information from Step # 4 - it will be way too much so remove
# or comment out those print statements
#