Monte carlo circle

Viewed 77

So I'm trying to draw a circle instead of a quarter circle does anyone knows how do I change the commands ? I want the center of the circle to be in the coordinates (0.5,0.5) , but I don't know how to change it. I know that the I need to change the radius to 0.5 but I can't figure out where I need to change the values for the center. Thanks!

import matplotlib.pyplot as plt
from matplotlib.pyplot import *
matplotlib.use("TkAgg")

fig = figure(figsize=(8, 8), dpi=120)

nTrials = int(100000)
radius = 1
nInside = 0
nDrops = 0

XrandCoords = np.random.default_rng().uniform(0, 1, (nTrials,))
YrandCoords = np.random.default_rng().uniform(0, 1, (nTrials,))

fig1 = plt.figure(1)
plt.get_current_fig_manager().window.wm_geometry("+00+00")  # move the window
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.legend()

isFirst1 = True
isFirst2 = True

piValueI = []
nDrops_arr = []

insideX = []
outsideX = []
insideY = []
outsideY = []

for i in range(nTrials):
    x = XrandCoords[i]
    y = YrandCoords[i]

    nDrops = nDrops + 1

    if (x ** 2 + y ** 2) <= radius ** 2:
        nInside = nInside + 1
        insideX.append(x)
        insideY.append(y)

    else:
        outsideX.append(x)
        outsideY.append(y)

    if i % 100 == 0:

        plt.figure(1)

        if isFirst1:

            plt.scatter(insideX, insideY, c='blue', s=50, label='În interior')
            isFirst1 = False
            plt.legend(loc=(0.75, 0.9))
        else:

            plt.scatter(insideX, insideY, c='blue', s=50)

        plt.figure(1)

        if isFirst2:

            plt.scatter(outsideX, outsideY, c='black', s=50, label='În exterior')
            isFirst2 = False
            plt.legend(loc=(0.75, 0.9))
        else:
            plt.scatter(outsideX, outsideY, c='black', s=50)

        area = 4 * nInside / nDrops
        plt.figure(1)
        plt.title('Nr de puncte luate = ' + str(nDrops) + ';  Nr punctelor din interiorul cercului = ' + str(
            nInside) + r';  π  ≈ $4\frac{Nr_\mathrm{interior}}{Nr_\mathrm{total}}=$ ' + str(np.round(area, 6)))
        piValueI.append(area)
        nDrops_arr.append(nDrops)

        plt.pause(0.1)

area = 4 * nInside / nTrials
print("Valoare estimată a lui Pi: ", area)
plt.show()
1 Answers

Please try :

radius = 0.5
XrandCoords = np.random.uniform(0.5, 1, (nTrials,))
YrandCoords = np.random.uniform(0.5, 1, (nTrials,))
radius = 0.5
if ((x-0.5) ** 2 + (y-0.5) ** 2) <= radius ** 2:
....
Related