Skip to content

Instantly share code, notes, and snippets.

@pinkmomo027
Created February 23, 2012 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinkmomo027/1890037 to your computer and use it in GitHub Desktop.
Save pinkmomo027/1890037 to your computer and use it in GitHub Desktop.
class PI
@@TIME = 10000000
attr :circle, :square
def initialize
@circle = 0
@square = 0
calculate_pi
end
def generate_length
a = rand(10).to_f/10
b = rand(10).to_f/10
Math.sqrt(a * a + b * b)
end
def calculate_pi
@@TIME.times do |t|
length = generate_length
if length >= 1
@square += 1
else
@square += 1
@circle += 1
end
end
puts @circle
puts @square
pi = @circle/@square.to_f * 4
end
end
pi = PI.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment