柔道の背負い投げは、
この背負い投げのコツを、
今回は、

連続事象
コインやさいころのように、

一様分布
最も簡単な連続事象の確率分布の例として、の紙テープを屋外に水平一直線に渡します。雨が降ってきたとき、
に雨粒が落ちる個数は、
の長さに比例します。このとき、
のテープに落ちる雨粒のうち、
の区間に落ちる雨粒の確率
は次の式で表されます。


モンテカルロ法
一様分布の連続事象をうまく利用した例にモンテカルロ法で円周率の値を求める方法があります。モンテカルロ法
正方形にぴったり内接する円をとります。この図形を屋外において、
円の中に落ちた雨粒の数と正方形の中に落ちた雨粒の全ての数の比は、


ただし、は正方形の一辺の長さであると同時に、
ここで、、

なんともシンプルな形となりました。式53.
問題 モンテカルロ法で円周率を求めましょう。
大変シンプルな問題ですから、
解説
問題 モンテカルロ法で円周率を求めましょう
コードを簡単にするために、

このように問題を読みかえても、

/*
問題 モンテカルロ法で円周率を求めましょう。
filename : Sample_MC.java
uses : Dice.class
*/
class Sample_MC {
public static void main(String[] args) {
int trial = 1000000; //試行回数
//Dice クラスを使った場合
int r = 1000; //円の半径
Dice dx = new Dice(r); //x 軸用ダイス
Dice dy = new Dice(r); //y 軸用ダイス
int hit = 0; //円の中の雨粒
double MC_Pi = 0.0; //円周率値
//Math.random を使った場合
int hit2 = 0; //円の中の雨粒
double MC_Pi2 = 0.0; //円周率値
//---------------------------------
System.out.println("モンテカルロ法"
+trial+"個雨粒を落とす。");
for (int i=0; i<trial; ++i){
if ( Math.pow(dx.roll(1),2)
+ Math.pow(dy.roll(1),2)
<= Math.pow(r,2) ) ++hit;
if ( Math.pow(Math.random(),2)
+ Math.pow(Math.random(),2)
<= 1 ) ++hit2;
if ( i % (trial/10) == 0 )
System.out.print("*");
}
System.out.println();
MC_Pi = (double) hit / trial * 4;
MC_Pi2 = (double) hit2 / trial * 4;
System.out.println("<試行結果>");
System.out.println("Dice クラス使用π="+MC_Pi);
System.out.println("Math クラス使用π="+MC_Pi2);
}// end of main
}// end of class Sample_MC
サンプルコードの実行結果を示します。
C:\>java Sample_MC
モンテカルロ法1000000 個雨粒を落とす。
**********
<試行結果>
Dice クラス使用π = 3.134884
Math クラス使用π = 3.140752
Mathクラスを使った方が若干精度が良いのは、
今回はここまで
今回は、