working of drand48()

P

pereges

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 = x; Y = y; Z = z;
}
}
 
I

Ian Collins

pereges said:
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
little help form google.

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

Antoninus Twink

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top