Search code examples
pythonpython-3.xmontecarlo

Monte Carlo dart simulator


I've been trying to make a dart simulator using the Monte Carlo simulation in Python 3. So far, I have written the following code:

import random
import math

n = (input("Enter the number of darts you have. "))
count = 1
circleval = 0
squareval = 0
while count <= n:
    y = 2*random.random()*2-1
    x = 2*random.random()*2-1
    if math.sqrt((x-1)**2+(y-1)**2)<=1:
        circleval+=1
        squareval+=1
    else:
        squareval+=1
    count+=1

print("Pi is " + 4*(1.*circleval/squareval))

However, when I run this I reccive the following error message:

TypeError: '<=' not supported between instances of 'int' and 'str'

Solution

  • The main reason why this does not work is because:

    n = (input("Enter the number of darts you have. "))
    

    will put a string into n, we can solve this with:

    n = int(input("Enter the number of darts you have. "))

    and:

    print("Pi is " + 4*(1.*circleval/squareval))
    

    expects a string, but you do not provide one, we can solve this with:

    print("Pi is " + str(4*(1.*circleval/squareval)))

    But that being said, the program is still incorrect: it gives 0.773 as output for Pi, which is clearly wrong.

    The main problem is your sampling: you want to generate numbers between -1 and 1, but you generate numbers between -1 and 3. In your distance computations, you then use x-1 and y-1 shifting it to the -2 to 2 domain, but that is still too large. Furthermore the code not very elegant.

    from random import random
    
    n = int(input("Enter the number of darts you have. "))
    c = 0
    for i in range(n):
        x = 2*random()-1
        y = 2*random()-1
        if x*x + y*y <= 1:
            c += 1
    print("Pi is %s" % (4.0*c/n))
    

    for n=100000, this has given me 3.14368 (although it might vary of course between several simulations).