## Monte Carlo & Python

Oooooootay!!!!! Now, we’re going to use Python to simulate the Monte Carlo concept. Now, what in God’s name is Monte Carlo? It’s a concept that builds upon the predictive analytics we’ve been doing.

Okay, to explain a Monte Carlo the best way is to go back to linear regressions. In linear regressions, we predict results based on the data we have in front of us, but what if that data fluctuates? Furthermore, what if the one time we use it’s for stats that maybe are on one end of the spectrum or the other? We’d get extreme results or outliers and not necessarily the results we’d get any other time.

To eliminate that we’d run a Monte Carlo which would run numbers at random, hundreds maybe even thousands of times, to weed out the extremes and give us a more accurate prediction.

The Monte Carlo itself was pioneered by the Manhattan Project scientists. They were testing nukes but didn’t know how much uranium they needed to test. Being that they had limited uranium this posed a serious problem. So, they ran Monte Carlos to find reliable numbers of material needed and reduced how much material they wasted.

Here we’re going to use Python to run Monte Carlos for us. We’re going to simulate the rolling of dice and see how many times we win and lose.

First, as always we import the tools we need to make this thing work. This time we only need one the *random* module. So, let’s import:

`import random`

What random does is just as it’s name suggests, it runs and chooses a random number.

This is going to be the basis for our method which will simulate a dice roll. First, let’s create our method to roll the dice.

`def rollDice():`

roll = random.randint(2,12)

return roll

Okay, *random.randint(2,12)* is the main worker in this. What this does is it chooses a random number based on the outcomes of a dice roll. In the parentheses are the outcomes of the dice roll. Then we assign that to the variable *roll*. We then return roll to be used later in the code.

Next, we’re set our counters. For my program, I’m going to use 3. One to keep count of how times the Monte Carlo has run, one to tabulate the wins, and another to tabulate the losses. Code is here:

`count = 0`

wins = 0

losses = 0

Next, we do another form of looping. This time we do a *while* loop. A while loop is just as it says it runs *while* whatever you’ve placed as it’s condition to keep running while your loop runs. So, here’s the code and I’ll go thru and explain.

`while count < 1000:`

result = rollDice()

if result < 8:

print ("You lose.")

losses+=1

else:

print ("You are a winner.")

wins+=1

print(result)

count+=1

Okay, so this says while the count is less than 1000, run the RollDice() method and save it to result. Then we use another loop to say if that result is less than 8, print out “You lose.” and add a 1 to the number of losses. If not, print out “You are the winner”and add to a 1 number of wins. With each ruling the program will also print out the result, then add 1 to the count. It will do this until it reaches 1000 and then stop.

Lastly, we summarize everything. We’ll use another loop that will change depending on whether we got more wins than losses or vice versa. Here’s the code:

`if wins > losses:`

print ("Congratualtions, you won "+ str(wins) +" times.")

print ("You lost "+str(losses)+" times.")

else:

print ("You won "+ str(wins) +" times.")

print ("However, you lost "+str(losses)+" times.")

```
```

`print ("Please try again.")`

Okay, what we did was had the program print out what we with the win variable that shows how many wins we counted. Then the same for the losses. Depending on which is more the output of the program changes. Then we add a “Goodbye” message and that’s that. We’ve made a program that runs Monte Carlos for us.

Here are the screens.