#!/usr/koeki/bin/ruby require 'gnuplot' srand() n = 10 # Ⱦ·Â m = 1000 # »î¹Ô²ó¿ô pt_x = Array.new pt_y = Array.new ct_r = 0 1.upto(m) do x = rand(n) y = rand(n) if x ** 2 + y ** 2 < 10 ** 2 ct_r += 1 end pt_x << x + 1 pt_y << y + 1 end printf("pi = #{ct_r.to_f / m * 4}\n") file = "dtd_pi.csv" func = "sqrt(#{n**2} - x ** 2)" Gnuplot.open do |gp| Gnuplot::Plot.new(gp) do |pl| pl.xrange "[0:11]" pl.yrange "[0:11]" pl.xlabel "x" pl.ylabel "y" pl.data << Gnuplot::DataSet.new( [pt_x, pt_y] ) do |ds| ds.notitle end pl.data << Gnuplot::DataSet.new(func) do |ds2| end end end