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.