情報
大学生・専門学校生・社会人

Pythonについて。
やろうとしてることはモンテカルロ法で円周率を求める際の横軸を試行回数、縦軸を円内に点を打った回数とした図の表示です。
グラフがおかしかったので、後述するプログラムに
print(ins)
を加えたところ0とでました。
どうすれば機能するのでしょうか?

import math
import numpy as np
import matplotlib.pyplot as plt

n = 100000

x = np.zeros([n])
y = np.zeros([n])
xin = np.zeros([n])
yin = np.zeros([n])
xout = np.zeros([n])
yout = np.zeros([n])

ins = 0
out = 0

def monte(n,ins): 
result_x = []
result_y = []
for i in range(n):
x[i] = 1-2*np.random.random()
y[i] = 1-2*np.random.random()

if x[i]**2 + y[i]**2 <= 1:
ins += 1

result_x.append(i)
result_y.append(ins)
return result_x, result_y

result = monte(n,ins)
result_x = result[0]
result_y = result[1]
plt.plot(result_x, result_y)
plt.xlim([0,n])
plt.ylim([0,ins])

plt.show()

回答

まだ回答がありません。

疑問は解決しましたか?

この質問を見ている人は
こちらの質問も見ています😉