On 22 June 2010 00:30, Yaser Sulaiman <yaserbuntu / gmail.com> 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/447554. > 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) => 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 <groups.2009a / grandfenwick.net> wrote: > > On Jun 21, 2010, at 15:45 , Caleb Clausen wrote: > >> On 6/19/10, Daniel Moore <yahivin / 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) >> ¨Âîçìå½òáî䪲ªÍáô躺ÐÉ >> ¨Âª½òáîä >> ¨Â«½òªÍáôè®óéî¨áîçìå>> ¨Â«½òªÍáôè®ãïó¨áîçìå>> >> ¨Âåôõòø¬>> 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. > 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 ðò^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