MonteCarloPI.rb
Code: "MonteCarloPI.rb". Programming language: Ruby DMelt Version 1.4. Last modified: 01/23/1973. License: Pro
https://datamelt.org/code/cache/MonteCarloPI_4364.rb
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


require "java"

java_import java.lang.Math;
java_import java.lang.System;

nThrows = 0; nSuccess = 0;
xthen = System.nanoTime();
events=1e8;
for i  in 0 .. events do
   x = Math.random(); y = Math.random();     #  Throw a dart                   
   nThrows +=1
   if ( x*x + y*y <= 1 )
                nSuccess += 1
  end
end

itime = (System.nanoTime() - xthen)/1e9;
xpi=(4.0*nSuccess)/nThrows
puts "Time for calculations (sec):  #{itime}"
puts "Pi = #{xpi}"


You see the box below because you did not login.