Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created May 3, 2012 22:14
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 practicingruby/2589898 to your computer and use it in GitHub Desktop.
Save practicingruby/2589898 to your computer and use it in GitHub Desktop.
class Point
def self.random(distance_range, randomizer=Random.new)
angle = randomizer.rand(0..2*Math::PI)
length = randomizer.rand(distance_range)
x = length*Math.cos(angle)
y = length*Math.sin(angle)
point = new(x, y)
center = new(0, 0)
if distance_range.include?(point.distance(center))
point
else
random(distance_range)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment