MonteCarloPI.rb
Source code name: "MonteCarloPI.rb"
Programming language: Ruby
Topic: Data mining/Algorithms
DMelt Version 1.4. Last modified: 01/23/1973. License: Pro
https://datamelt.org/code/cache/MonteCarloPI_2880.rb
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.


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}"


Ads help maintain this website.