Velocity Reviews > Ruby > [QUIZ] Random Points within a Circle (#234)

# [QUIZ] Random Points within a Circle (#234)

Daniel Moore
Guest
Posts: n/a

 06-19-2010
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

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
the original quiz message, if you can.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

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

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

Benoit Daloze
Guest
Posts: n/a

 06-21-2010
Hi !

On 19 June 2010 20:05, Daniel Moore <(E-Mail Removed)> 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

Caleb Clausen
Guest
Posts: n/a

 06-21-2010
On 6/19/10, Daniel Moore <(E-Mail Removed)> 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:I
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.

Dave Howell
Guest
Posts: n/a

 06-21-2010

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

> On 6/19/10, Daniel Moore <(E-Mail Removed)> wrote:
>> The quiz this week
>> is to generate random points uniformly distributed within a circle of
>> a given radius and position.

>=20
> def random_point_in_a_circle(x,y,r)
> angle=3Drand*2*Math:I
> r*=3Drand
> x+=3Dr*Math.sin(angle)
> y+=3Dr*Math.cos(angle)
>=20
> return x,y
> end
>=20
> 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.=20
=20

brabuhr@gmail.com
Guest
Posts: n/a

 06-22-2010
On Mon, Jun 21, 2010 at 7:12 PM, Dave Howell
<(E-Mail Removed)> wrote:
> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>> On 6/19/10, Daniel Moore <(E-Mail Removed)> 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)
>> =A0angle=3Drand*2*Math:I
>> =A0r*=3Drand
>> =A0x+=3Dr*Math.sin(angle)
>> =A0y+=3Dr*Math.cos(angle)
>>
>> =A0return 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 ran=

dom distance from the center. This will result in way too many points at th=
e center of the circle.

I whipped up a quick little visualization:

def random_point_in_a_circle(x,y,r)
angle=3Drand*2*Math:I
r*=3Drand
x+=3Dr*Math.sin(angle)
y+=3Dr*Math.cos(angle)

return x,y
end

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

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

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

10000.times do
x,y =3D 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 =3D RPwiaC.new
panel.repaint
panel.revalidate

brabuhr@gmail.com
Guest
Posts: n/a

 06-22-2010
On Mon, Jun 21, 2010 at 9:12 PM, <(E-Mail Removed)> wrote:
> On Mon, Jun 21, 2010 at 7:12 PM, Dave Howell
> <(E-Mail Removed)> wrote:
>> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>>> On 6/19/10, Daniel Moore <(E-Mail Removed)> 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)
>>> =A0angle=3Drand*2*Math:I
>>> =A0r*=3Drand
>>> =A0x+=3Dr*Math.sin(angle)
>>> =A0y+=3Dr*Math.cos(angle)
>>>
>>> =A0return 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 ra=

ndom distance from the center. This will result in way too many points at t=
he center of the circle.
>
> I whipped up a quick little visualization:

Quick hack to animate the above method side by side with an alternate metho=
d:

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

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

class RPwiaC < JPanel
def initialize times, *procs
super()

@times =3D times
@procs =3D procs
end

def paintComponent(graphics)
super(graphics)

@times.times do
@procs.each_with_index do |proc, i|
x,y =3D proc.call(200 + (300 * i),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
end

panel =3D RPwiaC.new(
5000,
lambda{|x,y,r|
angle=3Drand*2*Math:I
r*=3Drand
x+=3Dr*Math.sin(angle)
y+=3Dr*Math.cos(angle)
return x,y
},
lambda{|x,y,r|
loop {
a =3D x + rand(2*r) - r
b =3D y + rand(2*r) - r
d =3D Math.sqrt((x - a) ** 2 + (y - b) ** 2)
return a,b if d < r
}
}
)

panel.revalidate

loop { panel.repaint }

Caleb Clausen
Guest
Posts: n/a

 06-22-2010
On 6/21/10, Dave Howell <(E-Mail Removed)> wrote:
>
> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>> 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 guess this is why Benoit had that sqrt in there. I don't quite get
why it's necessary.

I kinda like Yaser's solution.

Benoit Daloze
Guest
Posts: n/a

 06-22-2010
On 22 June 2010 00:30, Yaser Sulaiman <(E-Mail Removed)> wrote:
> 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/44755=

4.
> Any feedback is welcomed.

I would say it is a good try, and the distinction Vector/Point is
interesting, while being a problem with DRY (you repeat the
calculation of the distance).
A quick tip a friend showed me: Math.sqrt(a**2 + b**2) =3D> Math.hypot(a, b=
)
In the end, you manually add the offset to x and y. As you have
Vector/Point, the Point+Vector should be defined and then the 4 last
lines would be: "center + v"

On 22 June 2010 01:12, Dave Howell <(E-Mail Removed)> wrote:
>
> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>
>> On 6/19/10, Daniel Moore <(E-Mail Removed)> 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)
>> =A0angle=3Drand*2*Math:I
>> =A0r*=3Drand
>> =A0x+=3Dr*Math.sin(angle)
>> =A0y+=3Dr*Math.cos(angle)
>>
>> =A0return 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 ran=

dom distance from the center. This will result in way too many points at th=
e center of the circle.
>

That was also my initial thought, and I think for most of us.

Caleb:
> I guess this is why Benoit had that sqrt in there. I don't quite get
> why it's necessary.

Indeed, please look at my solution and remove the "sqrt", and you will
see most of the points will be in the center .
The output will look like:
min: 1, moy, 1.13, max: 122 # This is way too much
max Point: (-2.0,4.0) # which is the center

The image would then look like a point in the center.
I 'felt' it would result in that because the points near the center
will be much closer to each other, as it will be as much points at any
distance.

Brabuhr's visualization is even *way* better to see it (especially the
animated one) !

And finally, why the sqrt? I just felt like I should have as many
points in each area, and the area of a circle is =F0r^2, so let's reduce
this r^2 and becomes r. (Also I wanted the random distance to be
likely further from the center, and as rand returns between 0 and 1,
we have to do ^(1/2) and not ^2.

- Benoit

brabuhr@gmail.com
Guest
Posts: n/a

 06-22-2010
2010/6/22 Benoit Daloze <(E-Mail Removed)>:
> On 22 June 2010 01:12, Dave Howell <(E-Mail Removed)> wrote:
>> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>>> On 6/19/10, Daniel Moore <(E-Mail Removed)> wrote:
>>>> The quiz this week
>>>> is to generate random points uniformly distributed within a circle of
>>>> a given radius and position.

>
> Caleb:
>> I guess this is why Benoit had that sqrt in there. I don't quite get
>> why it's necessary.

>
> The image would then look like a point in the center.
> I 'felt' it would result in that because the points near the center
> will be much closer to each other, as it will be as much points at any
> distance.
>
> And finally, why the sqrt? I just felt like I should have as many
> points in each area, and the area of a circle is =F0r^2, so let's reduce
> this r^2 and becomes r. (Also I wanted the random distance to be
> likely further from the center, and as rand returns between 0 and 1,
> we have to do ^(1/2) and not ^2.

Found an explanation:
http://www.anderswallin.net/2009/05/...-circle-using=
-polar-coordinates/

Caleb Clausen
Guest
Posts: n/a

 06-22-2010
On 6/22/10, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> Found an explanation:
> http://www.anderswallin.net/2009/05/...r-coordinates/

Thanks. It took me a while, but it's making sense now.