Monte Carlo calculation of PI
Source code name: "MonteCarloPI.groovy"
Programming language: Groovy
Topic: Data mining/Algorithms
DMelt Version 1.4. Last modified: 01/21/1973. License: Free
https://datamelt.org/code/cache/MonteCarloPI_5508.groovy
To run this script using the DataMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


/*
 A Monte Carlo experiment to find an estimate for Pi 
 Computing the probability that a dart hits the yellow portion of the dart board: 
 Probability [ a portion of circle] = Area of the quarter circle    = pi/4 
*/

int nThrows = 0;
int nSuccess = 0;
double x, y;
long then = System.nanoTime();
int events=(int)1e8;
for (int i = 0; i < events; i++)   {
   x = Math.random(); y = Math.random();     // Throw a dart                   
   nThrows++;
   if ( x*x + y*y <= 1 )  nSuccess++;
}
int itime = (int)((System.nanoTime() - then)/1e9);
System.out.println("Time for calculations (sec): " + itime+"\n");
System.out.println("Pi = " + 4*(float)nSuccess/(float)nThrows +"\n");