Created
January 6, 2016 07:56
-
-
Save ryuichimatsumoto-single/56a3f99e0b2226941a31 to your computer and use it in GitHub Desktop.
モンテカルロ法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- 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