モンテカルロ法で楕円の面積を求める。

モンテカルロ法で図形の面積を求めることができる。

x*x/4+y*y=1
の式で示される楕円の面積をモンテカルロ法で求める。

乱数 0≦x≦2 , 0≦y≦1 を2×1の長方形の中に均一に落とす。
1/4の楕円の中に入った乱数の数をr
落とした乱数の総数をn
1/4の楕円の面積をSとすると
2 : S / 4 = n : r
S = 4 * 2 * r / n
となる。

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<climits>
#include<cmath>

using namespace std;

typedef unsigned int uint;

uint xor128(void) { 
  static uint x = 123456789;
  static uint y = 362436069;
  static uint z = 521288629;
  static uint w = 88675123; 
  uint t;
  
  t=x^(x<<11);
  x=y; y=z; z=w;
  return w=(w^(w>>19))^(t^(t>>8)); 
}

int main(void){
	
	double x,y,r,n;
	
	cin >> n;
	
	for(int i=0;i<n;i++){
		double x=2*(double)xor128()/UINT_MAX;
		double y=(double)xor128()/UINT_MAX;
		
		if(sqrt(x*x/4+y*y)<=1.0)r++;
	}
	
	cout << 8*r/n << endl;
	
return 0;
}