Re: [Algorithms] It's all Random Numbers
Brought to you by:
vexxed72
|
From: Kent Q. <ken...@co...> - 2000-10-31 13:44:06
|
If you generate random points in a cube and reject those outside the radius of the sphere, the points will be randomly distributed within the VOLUME of the sphere. If you do that, just under half the candidate points will be rejected. To generate points evenly distributed around the surface of a sphere using spherical coordinates, you can easily choose theta as just a random value from 0 to 2*pi. But to avoid bunching at the poles, you need to choose phi (the latitude) according to the inverse of the length of that latitude line, which is proportional to cos(phi). So: theta = random(0, 2*pi) v = random(-2*pi, 2*pi) phi = v / cos(phi) Now if you want to use the spherical coordinates to generate a volume within the sphere, you do the above, and then you have to pick a random radius according to inverse of the area at the given radius, so, as you surmise: r = random ^ (1/3) If you need points in a spherical volume, I think the rejection test is the best way to go. Kent Ludvig Tunel wrote: > > Hi, > > I came to think of a very similar problem: > How do I generate random points in a sphere, ( or on the sphere surface ) > using spherical polar coordinates? > > If i randomize azimuth and inclination I will get denser distribution around > the "poles". > I would need to transform the randomized[-pi/2 ... pi/2] inclination in some > way, but how? > Taking the square root doesn't feel like the right thing to do here. It feels > more like I should feed it to cos() or sin() in some way... > The probability of generating a point at a certain inclination must somehow > be transformed to be proportional to the area of the disc at the given > inclination. Right? > But how? > > (The problem of denser distribution close to the centre could as I assume > from having read the 2D solution, be solved by taking the cube root of the > radius. ( radius^(1/3) ) Am I right?) > > cheers > Ludde > > Gil Gribb wrote: > > > > Hi, > > > Thanks for replying. > > > > > > That works spot-on! > > > > > > I was doing everything except the sqrt. > > > Is it trivial to explain why this method works? > > > > Sure, the area of a disk is proportial to the radius squared. And so the > > sqrt "undoes" this. > > -Gil > > > > > > > > Cheers for your time > > > Zaf > > > > > > > > > -----Original Message----- > > > From: Gil Gribb [mailto:gg...@ma...] > > > Sent: 25 October 2000 15:14 > > > To: gda...@li... > > > Subject: Re: [Algorithms] It's all Random Numbers > > > > > > > > > > > > > > > > Hi All, > > > > Does anyone know a fast way of calculating 2D points inside a > > > > unit-disc, in a way that leads to a roughly even distribution? > > > > > > Well, the usual way is to just generate points in [-1,1]x[-1,1] and just > > > throw away points that are outside the disk. But, I think this will work > > > too: > > > > > > angle = 2 pi random() > > > r = sqrt(random()) > > > > > > x= r cos(angle) > > > y= r sin(angle) > > > > > > -Gil > > > > > _______________________________________________ > GDAlgorithms-list mailing list > GDA...@li... > http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list -- ----------------------------------------------------------------------- Kent Quirk | CogniToy: Intelligent toys... Game Architect | for intelligent minds. ken...@co... | http://www.cognitoy.com/ _____________________________|_________________________________________ |