Created
February 23, 2012 04:10
-
-
Save pinkmomo027/1890037 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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