Skip to content

Instantly share code, notes, and snippets.

@ryuichimatsumoto-single
Created January 6, 2016 07:56
Show Gist options
  • Select an option

  • Save ryuichimatsumoto-single/56a3f99e0b2226941a31 to your computer and use it in GitHub Desktop.

Select an option

Save ryuichimatsumoto-single/56a3f99e0b2226941a31 to your computer and use it in GitHub Desktop.
モンテカルロ法
# -*- coding:utf-8 -*-
import numpy
import pylab
import math
import time
X = 0 # 的に当たった回数
N = 500 # 試行回数
# N回の試行を開始
for i in range(1, N):
score_x = numpy.random.rand()
score_y = numpy.random.rand()
if score_x * score_x + score_y * score_y < 1:
X = X + 1
# piの近似値をここで計算する
pi = 4*float(X)/float(i)
plot_x = i
plot_y = pi
pylab.plot(plot_x,plot_y,"ro")
# 結果を表示
pylab.grid(True)
pylab.xlabel('X')
pylab.ylabel('Y')
pylab.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment