Velocity Reviews > working of drand48()

# working of drand48()

pereges
Guest
Posts: n/a

 04-12-2008
I came across a code that calculates N uniformly distributed points on
the sphere. z is a random number between -1 and 1 and to calculate it
we use drand48 function which returns a uniformly distributed value
between 0 and 1. What I don't understand is how it actually works ?
What does he actually mean by "uniformly distributed values" ?

void SpherePoints(int n, double X[], double Y[], double Z[])
{
int i;
double x, y, z, w, t;

for( i=0; i< n; i++ ) {
z = 2.0 * drand48() - 1.0;
t = 2.0 * M_PI * drand48();
w = sqrt( 1 - z*z );
x = w * cos( t );
y = w * sin( t );
printf("i=%d: x,y,z=\t%10.5lf\t%10.5lf\t%10.5lf\n", i, x,y,z);
X[i] = x; Y[i] = y; Z[i] = z;
}
}

Ian Collins
Guest
Posts: n/a

 04-12-2008
pereges wrote:
> I came across a code that calculates N uniformly distributed points on
> the sphere. z is a random number between -1 and 1 and to calculate it
> we use drand48 function which returns a uniformly distributed value
> between 0 and 1. What I don't understand is how it actually works ?
> What does he actually mean by "uniformly distributed values" ?
>

Your man pages for drand48() and friends should explain this, with a

Note drand48() isn't a standard C function.

--
Ian Collins.

Antoninus Twink
Guest
Posts: n/a

 04-12-2008
On 12 Apr 2008 at 5:06, pereges wrote:
> I came across a code that calculates N uniformly distributed points on
> the sphere. z is a random number between -1 and 1 and to calculate it
> we use drand48 function which returns a uniformly distributed value
> between 0 and 1. What I don't understand is how it actually works ?
> What does he actually mean by "uniformly distributed values" ?

An interesting question.

The uniform measure on a finite set X assigns each point measure 1/|X|,
while the uniform measure on a continuous interval [a,b] has a
fairly complicated description - you're really building the Lebesgue
integral.

In this case, there are a couple of sets to consider. First is the
mathematician's interval [0,1] inside the real line. Then there's the
subset X of all those real numbers (each of which is actually rational)
that can be represented in the floating-point system being used. This is
a finite set, but it's hard to conceive of a random number generator
that could produce the uniform measure on X.

A weaker thing to hope would be that if one chooses any interval [a,b]
inside [0,1] with b-a "reasonably" sized (where "reasonable" has some
interpretation in terms of DBL_EPSILON) then as n->infinity, the
proportion of numbers generated that lie inside [a,b] converges to b-a.

Of course, what happens is that the random number generator is entirely
discrete, and its name suggests that it generates integers mod 2^48 and
then maps these to floating point numbers in [0,1]. If the generator is
a good one, then the proportion of outputs equal to any given number mod
2^48 will tend (as the number of iterations -> infinity) to 1/2^48.