How do casinos earn money? The trick is simple- you play long enough, the probability of losing money increases. Let us take a look at how this works with a simple Monte Carlo simulation.

Monte Carlo simulationis a technique used to understand the impact of risk and uncertainty in financial, project management, cost, and other forecasting models. A Monte Carlo simulator helps one visualize most or all of the potential outcomes to have a better idea regarding the risk of a decision.

Consider an imaginary game in which our player ‘Jack’, rolls an imaginary dice to get an outcome of 1 to 100. If Jack rolls anything from 1–51, the house wins, but if the number rolled is from 52–100, Jack wins. Simple enough?

Before simulating the outcomes, let’s calculate the house edge. The house edge displays the advantage the casino has in winning the bet. **It’s the casino’s average profit from a player’s bet.**

Let’s suppose Jack bets $1 on this game.

Probability of Jack winning = 49/100

Probability of the casino winning = 51/100

Expected profit for the player= 1*(49/100)-1*(51/100) = -0.02 = -2%

Therefore, **the house edge in our imaginary game is 2%. **For comparison, the lowest house edge in European Roulette is 2.7%. In short, we have a better chance of winning in our imaginary game than in Roulette.

Now, let’s simulate different scenarios in Python to visualize the different outcomes if the player kept betting continuously.

**1.Import the required libraries**

#Import libraries

import random

import matplotlib.pyplot as plt

**2.We need a dice simulator which throws a value from 1–100 with uniform probability distribution. Let’s create a function in Python which returns ‘True’ if the player wins and ‘False’ if the Casino wins.**

#Create function for simulating die roll

#The die can take values from 1 to 100. If the number is between 1 #and 51, the house wins.

#If the number is between 52 and 100, the player wins.

def rolldice():

dice = random.randint(1,100)

if dice <=51:

return False

elif dice >51 & dice <=100:

return True

**3.Create a function that simulates the bets. We need to provide three arguments for the function :**

i. Total funds : The money the player is starting with ($10,000)

ii. Wager Amount : The amount the player bets in each game ($100)

iii. Total plays : The number of times the player plays the game (This value is changed for creating different scenarios)

#Define a function for the play which takes 3 arguments :

#1. total_funds = total money in hand the player is starting with

#2. wager_amount = the betting amount each time the player plays

#3. total_plays = the number of times the player bets on this game

def play(total_funds, wager_amount, total_plays):

#Create empty lists for :

# 1.Play_number and

# 2.Funds available

# 3.Final Fund

Play_num = []

Funds = []

#Start with play number 1

play = 1

#If number of plays is less than the max number of plays we have set

while play < total_plays:

#If we win

if rolldice():

#Add the money to our funds

total_funds = total_funds + wager_amount

#Append the play number

Play_num.append(play)

#Append the new fund amount

Funds.append(total_funds)

#If the house wins

else:

#Add the money to our funds

total_funds = total_funds - wager_amount

#Append the play number

Play_num.append(play)

#Append the new fund amount

Funds.append(total_funds)

#Increase the play number by 1

play = play + 1

#Line plot of funds over time

plt.plot(Play_num,Funds)

Final_funds.append(Funds[-1])

return(Final_funds)

**4. Finally, run a loop to call the above functions and simulate the game for multiple scenarios. To be ****confident**** of the end results of our game, each scenario will be simulated 100 times.**

#Call the function to simulate the plays and calculate the remaining #funds of the player after all the bets

#Intialize the scenario number to 1

x=1

#Create a list for calculating final funds

Final_funds= []

while x<=100:

ending_fund = play(10000,100,5)

x=x+1

#Plot the line plot of "Account Value" vs "The number of plays"

plt.ylabel('Player Money in $')

plt.xlabel('Number of bets')

plt.show()

#Print the money the player ends with

print("The player starts the game with $10,000 and ends with $" + str(sum(ending_fund)/len(ending_fund)))

**5. Let’s visualize 7 different scenarios using the following graphs. In each scenario Jack bets n number of times.**

For generating multiple scenarios, use the **above** block of code (#4), but only modify the highlighted code (shown below) to tweak the number of bets the player makes.

ending_fund = play(10000,100,5)

x-axis : The number of bets Jack makes

y-axis : Jack’s account balance after each bet

**Each graph shows Jack’s account balance as he continues to play the game. Also, to find out Jack’s ending balance, we would be averaging out the balance from the 100 different simulations.**

The aim of this experiment is to answer a simple question- “if a person starts with $10,000 and plays this game n number of times (and places a bet of $100 in each game), on an average, how much money would the player end up with ?”

**Scenario 1 -> No. of bets : 5**

**Scenario 2 -> No. of bets : 10**

**Scenario 3 -> No. of bets : 50**

**Scenario 4 -> No. of bets: 100**

**Scenario 5 -> No. of bets : 500**

### Scenario 6 -> No. of bets : 1000

**Scenario 7 -> No. of bets : 10000**

From the simulation experiment, we can observe that Jack has a better chance of making a profit (or minimize loss), if he places fewer bets.

**It can also be observed that the amount lost in every scenario is approximately 2% of the betting amount (same as the house edge)**. For example, in our final scenario — Jack made 10,000 bets, each for $100. So we would expect Jack to lose approximately (10,000)*(0.02*100), which amounts to $20,000.

Moreover, in the last scenario when he plays 10,000 times, the funds have become negative, i.e Jack has lost more money than what he started with (In real life, it means Jack lost all his money).

In short,** THE HOUSE ALWAYS WINS !**

**Resources** :

1. https://pythonprogramming.net/monte-carlo-simulator-python/

2. http://math.info/Misc/House_Edge/

Connect on **LinkedIn**.

Please find the Python notebook embedded below :

Source: Deep Learning on Medium