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


Hi,

On Fri, 05 Aug 2005 23:59:04 +0200
Jochen Küpper <jochen@fhi-berlin.mpg.de> wrote:

> I need to calculate 2d random numbers uniformly distributed over a
> circle (uncorrelated).

if x_i (i=1, ...,n) are n random numbers with Gaussian distribution then
the vector

  r = 1/sqrt(x_1² + ... + x_n²) * (x_1, ..., x_n)

is uniformly distributed _on_ an n-dimensional spheere. The vector

  t = u^(1/n)*r

is uniformly distributed _in_ an n-dimensional spheere, where u is a
random number uniformly distributed in [0, 1]. 

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

> Currently I am calculating a uniform distribution over a square and
> reject the values outside the circle:
> ,----
> | 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. 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);


	Heiko

--
-- Es ist besser, für etwas zu kämpfen, als gegen etwas.
-- (Amos Bronson Alcott)
-- Cluster Computing @ http://www.clustercomputing.de
--       Heiko Bauke @ http://www.uni-magdeburg.de/bauke

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]