0

Hey so I'm relatively new and I'm having trouble running the Monte Carlo simulation and printing the results:

import random
import math
def computePI(throws):
throws = 100
radius = 1
ontarget = 0
offtarget = 0
numthrows = 0
while throws < 10000000:
    while numthrows < throws:
        x = random.uniform(-1.0,1.0)
        y = random.uniform(-1.0,1.0)
        hyp = math.hypot(x,y)
        if hyp <= radius:
            ontarget += 1
            numthrows+=1
        else:
            offtarget += 1
            numthrows+=1
        continue
    pi = (ontarget/throws)*4
    throws *= 10
    return(pi)

def main ():
throws = 100
while throws <= 10000000:
    difference = computePI(throws) - math.pi
    print('{first} {last}'.format(first="Num =", last=throws),end = "    ")
    print('{first} {last}'.format(first="Calculated Pi =", last=computePI(throws)),end = "    ")
    if difference < 0:
        print('{first} {last}'.format(first="Difference =", last=round(difference,6)))


    if difference > 0:
        print('{first} +{last}'.format(first="Difference =", last=round(difference,6)))
    throws *= 10
 main()

So I think the Monte Carlo function (computePI) is correct. I'm trying to run the Monte Carlo function for the values 100, 1000,100000,1000000, and 10000000.Is there a way to run the computePI function everytime the while loop in the main() function loops?

Alan Hao
  • 3
  • 1

1 Answers1

0

Your problem is white space:

1) You need to indent the body of computePi. If you are using IDLE this is easy: Highlight the body and use Ctrl + [

2) You need to indent the body of main

3) The final call to main() at the bottom of the file shouldn't have a space in front of it.

I made those changes and it ran as expected (though the approximations to pi were not particularly good).

On edit: The logic of your computePi didn't quite make sense. Try the following version:

def computePI(throws):
    radius = 1
    ontarget = 0
    offtarget = 0
    numthrows = 0
    for throw in range(throws):
        x = random.uniform(-1.0,1.0)
        y = random.uniform(-1.0,1.0)
        hyp = math.hypot(x,y)
        if hyp <= radius:
            ontarget += 1
            numthrows+=1
        else:
            offtarget += 1
            numthrows+=1
    pi = (ontarget/throws)*4
    return(pi)

def main ():
    throws = 100
    while throws <= 10000000:
        difference = computePI(throws) - math.pi
        print('{first} {last}'.format(first="Num =", last=throws),end = "    ")
        print('{first} {last}'.format(first="Calculated Pi =", last=computePI(throws)),end = "    ")
        if difference < 0:
            print('{first} {last}'.format(first="Difference =", last=round(difference,6)))


        if difference > 0:
            print('{first} +{last}'.format(first="Difference =", last=round(difference,6)))
        throws *= 10
main()

The code now gives fairly reasonable approximations to pi:

Num = 100    Calculated Pi = 3.4    Difference = -0.141593
Num = 1000    Calculated Pi = 3.124    Difference = +0.082407
Num = 10000    Calculated Pi = 3.106    Difference = -0.001593
Num = 100000    Calculated Pi = 3.13428    Difference = +0.012247
Num = 1000000    Calculated Pi = 3.14062    Difference = -0.000737
Num = 10000000    Calculated Pi = 3.14187    Difference = +0.000475
John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • Is there something wrong with the computePI function? How do I change the "throws" value everytime I run the computePI function? – Alan Hao Oct 05 '15 at 01:51
  • @AlanHao See if this version works better -- you needed your `throws` variable to count the number of times the main loop is executed – John Coleman Oct 05 '15 at 02:02
  • Ohh okay. So variables defined in main() can be used in other functions, but it doesn't work vice versa? – Alan Hao Oct 05 '15 at 02:08
  • No variables in `main` are used in `computePi`. You pass the *value* of `throws` from `main` to `computePi` - but the variable `throws` in `computePi's` scope isn't the same as the variable `throws` in `main`. Your code doesn't use any nonlocal variables. The only communication between `main` and `computePi` is via explicitly passed and returned values. – John Coleman Oct 05 '15 at 02:20