#include #include "TRandom.h" #include "TMath.h" using namespace std; void pi() { // hit and miss method UInt_t hit = 0; for( UInt_t i = 0; i < 1e9; ++i ) { // two uniform random numbers for xy position on square Double_t x = gRandom->Uniform(); Double_t y = gRandom->Uniform(); // check if radius is within circle if( x*x + y*y <= 1 ) { ++hit; } if( i % 10000000 == 0 ) { Double_t pi = 4. * Double_t( hit ) / Double_t( i+1 ); printf( "Event %d\tpi_mc = %10.8f\tpi = %10.8f\n", i, pi, TMath::Pi() ); } } }