Prev: Ruby Basic
Next: Ruby Basic
From: Benoit Daloze on
Hi !

On 19 June 2010 20:05, Daniel Moore <yahivin(a)gmail.com> wrote:
> ## Random Points within a Circle (#234)
>
> Greetings Rubyists,
>
> Generating random numbers is a useful feature and Ruby provides us
> with `Kernel.rand` to generate numbers uniformly distributed between
> zero and one. If we want to generate random numbers with other
> distributions, then we'll need to do a little work. The quiz this week
> is to generate random points uniformly distributed within a circle of
> a given radius and position.
>
> This quiz is relatively simple, great if you are new to Ruby or
> strapped for time. Remember, it is contributions from people like
> *you* that make Ruby Quiz succeed!
>
> Have fun!
>
> -Daniel

So here is my solution in a hundred lines, with an ImageMagick visualisation:

http://gist.github.com/446707

I had an intuition doing some Math.sqrt about the distance, and it
revealed to be exact :)

Enjoy,
Benoit Daloze

From: Yaser Sulaiman on
[Note: parts of this message were removed to make it a legal post.]

It's not as advanced as Daloze's solution, and it definitely can (should?)
be enhanced, but my solution is available at http://gist.github.com/447554.
Any feedback is welcomed.

In a nutshell, Point.random implements a random algorithm that
basically repositions the head of an initial null vector v until it falls
within a circle of the given radius that is centered around the origin. It
then translates a copy of that head to the correct position with respect to
the given center.

BTW, this is the first time I participate in the Ruby Quiz. I'm looking
forward to future quizzes :)

Regards,
Yaser Sulaiman

On Sat, Jun 19, 2010 at 9:05 PM, Daniel Moore <yahivin(a)gmail.com> wrote:

> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> The three rules of Ruby Quiz:
>
> 1. Please do not post any solutions or spoiler discussion for this
> quiz until 48 hours have elapsed from the time this message was
> sent.
>
> 2. Support Ruby Quiz by submitting ideas and responses
> as often as you can.
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem
> helps everyone on Ruby Talk follow the discussion. Please reply to
> the original quiz message, if you can.
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
> RSS Feed: http://rubyquiz.strd6.com/quizzes.rss
>
> Suggestions?: http://rubyquiz.strd6.com/suggestions
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Random Points within a Circle (#234)
>
> Greetings Rubyists,
>
> Generating random numbers is a useful feature and Ruby provides us
> with `Kernel.rand` to generate numbers uniformly distributed between
> zero and one. If we want to generate random numbers with other
> distributions, then we'll need to do a little work. The quiz this week
> is to generate random points uniformly distributed within a circle of
> a given radius and position.
>
> This quiz is relatively simple, great if you are new to Ruby or
> strapped for time. Remember, it is contributions from people like
> *you* that make Ruby Quiz succeed!
>
> Have fun!
>
> --
> -Daniel
> http://rubyquiz.strd6.com
>
>

From: Caleb Clausen on
On 6/19/10, Daniel Moore <yahivin(a)gmail.com> wrote:
> The quiz this week
> is to generate random points uniformly distributed within a circle of
> a given radius and position.

def random_point_in_a_circle(x,y,r)
angle=rand*2*Math::PI
r*=rand
x+=r*Math.sin(angle)
y+=r*Math.cos(angle)

return x,y
end

7 lines, 5 minutes, 0 tests or visualizations. Super-easy.

From: Dave Howell on

On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:

> On 6/19/10, Daniel Moore <yahivin(a)gmail.com> wrote:
>> The quiz this week
>> is to generate random points uniformly distributed within a circle of
>> a given radius and position.
>
> def random_point_in_a_circle(x,y,r)
> angle=rand*2*Math::PI
> r*=rand
> x+=r*Math.sin(angle)
> y+=r*Math.cos(angle)
>
> return x,y
> end
>
> 7 lines, 5 minutes, 0 tests or visualizations. Super-easy.

And wrong, unfortunately. You're selecting a random angle, and then a random distance from the center. This will result in way too many points at the center of the circle.



From: brabuhr on
On Mon, Jun 21, 2010 at 7:12 PM, Dave Howell
<groups.2009a(a)grandfenwick.net> wrote:
> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>> On 6/19/10, Daniel Moore <yahivin(a)gmail.com> wrote:
>>> The quiz this week
>>> is to generate random points uniformly distributed within a circle of
>>> a given radius and position.
>>
>> def random_point_in_a_circle(x,y,r)
>>  angle=rand*2*Math::PI
>>  r*=rand
>>  x+=r*Math.sin(angle)
>>  y+=r*Math.cos(angle)
>>
>>  return x,y
>> end
>>
>> 7 lines, 5 minutes, 0 tests or visualizations. Super-easy.
>
> And wrong, unfortunately. You're selecting a random angle, and then a random distance from the center. This will result in way too many points at the center of the circle.

I whipped up a quick little visualization:

def random_point_in_a_circle(x,y,r)
angle=rand*2*Math::PI
r*=rand
x+=r*Math.sin(angle)
y+=r*Math.cos(angle)

return x,y
end

require 'java'
JFrame = javax.swing.JFrame
JPanel = javax.swing.JPanel

frame = JFrame.new("Random Points within a Circle")
frame.default_close_operation = JFrame::EXIT_ON_CLOSE
frame.set_size(400, 400)
frame.show

class RPwiaC < JPanel
def paintComponent(graphics)
super(graphics)

10000.times do
x,y = random_point_in_a_circle(200,200,150)
graphics.draw_line(x-1,y-1,x+1,y+1)
graphics.draw_line(x-1,y+1,x+1,y-1)
end
end
end

panel = RPwiaC.new
frame.add(panel)
panel.repaint
panel.revalidate

 |  Next  |  Last
Pages: 1 2 3 4
Prev: Ruby Basic
Next: Ruby Basic