Thread: working of drand48()

  1. #1
    Registered User
    Join Date
    Apr 2008

    working of drand48()

    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 ? I see that in this function it has been called twice which would mean it return two different values each time. Wouldn't this lead to a wrong result ? I think what we can do is call it only once and store the random value in a variable and then use it in calculations. what do you think ?

    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;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    This is the Box-Muller method of generating numbers from a normal distribution from two(!) uniform random numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM