circular random number distribution

Heiko Bauke heiko.bauke@physik.uni-magdeburg.de
Fri Aug 5 22:40:00 GMT 2005


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/gsl-discuss/attachments/20050805/b32d0c96/attachment.sig>


More information about the Gsl-discuss mailing list