This is the mail archive of the gsl-discuss@sourceware.org mailing list for the GSL project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: circular random number distribution


Thanks to both of you that responded.

> Jochen Küpper <jochen@fhi-berlin.mpg.de> wrote:
>
>> I need to calculate 2d random numbers uniformly distributed over a
>> circle (uncorrelated).

Daniel Franke <daniel.franke@imbs.uni-luebeck.de> writes:

> try to sample random polar coordinates (r, phi) where r from [0,
> r_max] and phi from [-M_PI, M_PI]. Transform these polar coordinates
> to cartesian (x,y) afterwards.

AFAICT that is identical to using gsl_ran_dir_2d, which yields a
density peaked at the center of the circle. (See my original posting
and the link given below.)

Heiko Bauke <heiko.bauke@physik.uni-magdeburg.de> writes:

>   t = u^(1/n)*r
>
> is uniformly distributed _in_ an n-dimensional spheere, where u is a
> random number uniformly distributed in [0, 1]. 

Thanks, interesting.

> Set n=2 to get a solution for your problem. See also
> http://mathworld.wolfram.com/SpherePointPicking.html

Cool link. Here the even the special case is described:
,----
| http://mathworld.wolfram.com/DiskPointPicking.html
`----

>> ,----
>> | do {
>> |     x = gsl_ran_flat(rng, -1, 1);
>> |     y = gsl_ran_flat(rng, -1, 1);
>> | } while(sqrt(x*x + y*y) > 1.);
>> `----
>
> In practice this might be faster than the technique described above.

Well, probably. I'll test it on some machines. Does anybody have some
experience with respect to performance here?

> Try also
>
> phi = gsl_ran_flat(rng, 0, 2.0*PI);
> r = gsl_ran_flat(rng, 0, 1);
> x = r*cos(phi);
> y = r*sin(phi);

That comes down to the same problem as above. The correct solution,
given at http://mathworld.wolfram.com/DiskPointPicking.html, is
,----
| phi = gsl_ran_flat(rng, 0, 2.0*PI);
| r = gsl_ran_flat(rng, 0, 1);
| x = sqrt(r)*cos(phi);
| y = sqrt(r)*sin(phi);
`----

Greetings,
Jochen
-- 
Einigkeit und Recht und Freiheit                http://www.Jochen-Kuepper.de
    Liberté, Égalité, Fraternité                GnuPG key: CC1B0B4D
        (Part 3 you find in my messages before fall 2003.)

Attachment: pgp00000.pgp
Description: PGP signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]