Hi! I am a college student and self studying python by using the "how to think like a computer scientist-interactive version". In the lab session in this book, I need to approximate the value of pi by using Monte Carlo Simulation. I write the following codes but I am not sure if it is right. It did give me the result of pi, but the graph did not seem right. All the 10000 dots seem to concentrate at 4 corners (looks like 4 black squares), which concerns me. I read some "answers" to this question on the internet and nobody wrote similar codes like I did. Could anyone tell me if I have made a mistake? Thanks!
import turtle import math import random fred = turtle.Turtle() wn = turtle.Screen() wn.setworldcoordinates(-2,-2,2,2) wn.tracer(100) fred.up() numdarts = 10000 total=0 for i in range(numdarts): randx = random.random() randy = random.random() if randx>0.5 and randy>0.5: x=2*randx y=2*randy else: if randx>0.5 and randy<0.5: x=2*randx y=(-2)*randy else: if randx<0.5 and randy>0.5: x=(-2)*randx y=2*randy else: if randx<0.5 and randy<0.5: x=(-2)*randx y=(-2)*randy else: x=0 y=0 fred.goto(x,y) fred.stamp() dis=fred.distance(0,0) if dis<=2: total=total+1 else: total=total+0 result=total/numdarts pi=result*4 print("The value of pi is",pi) wn.exitonclick()