implementation of drand48() as given in steve summit's book

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pereges

    implementation of drand48() as given in steve summit's book

    I just tried to write a small program based on it :


    #include <stdio.h>
    #include <stdlib.h>

    #define PRECISION 2.82e14

    double drand48(void)
    {
    double x = 0;
    double denom = RAND_MAX + 1;
    double need;

    for(need = PRECISION; need 1; need /= (RAND_MAX + 1.))
    {
    x += rand()/denom;
    denom *= RAND_MAX + 1. ;
    }

    return x;

    }


    int main(void)
    {
    double x;
    x = drand48();
    printf("%f" ,x);
    x = drand48();
    printf("%f", x);

    return 0;

    }


    But each time I get the same output 0. What could be wrong here ?
    Does it mean that this will give teh same values over and over again
    everytime its called ?
  • jacob navia

    #2
    Re: implementation of drand48() as given in steve summit's book

    pereges wrote:
    I just tried to write a small program based on it :
    >
    >
    #include <stdio.h>
    #include <stdlib.h>
    >
    #define PRECISION 2.82e14
    >
    double drand48(void)
    {
    double x = 0;
    double denom = RAND_MAX + 1;
    double need;
    >
    for(need = PRECISION; need 1; need /= (RAND_MAX + 1.))
    {
    x += rand()/denom;
    denom *= RAND_MAX + 1. ;
    }
    >
    return x;
    >
    }
    >
    >
    int main(void)
    {
    double x;
    x = drand48();
    printf("%f" ,x);
    x = drand48();
    printf("%f", x);
    >
    return 0;
    >
    }
    >
    >
    But each time I get the same output 0. What could be wrong here ?
    Does it mean that this will give teh same values over and over again
    everytime its called ?
    Using lcc-win I obtain

    0.001268 0.585006

    Of course I inserted a space after the first number.
    It seems to be working

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique

    Comment

    • Eric Sosman

      #3
      Re: implementation of drand48() as given in steve summit's book

      pereges wrote:
      I just tried to write a small program based on it :
      >
      >
      #include <stdio.h>
      #include <stdlib.h>
      >
      #define PRECISION 2.82e14
      >
      double drand48(void)
      {
      double x = 0;
      double denom = RAND_MAX + 1;
      Are you sure this is copied correctly? As written here,
      this is likely to misbehave if RAND_MAX == INT_MAX; to fix
      that, change the final `1' to `1.0'.

      --
      Eric.Sosman@sun .com

      Comment

      • Barry Schwarz

        #4
        Re: implementation of drand48() as given in steve summit's book

        On Mon, 21 Apr 2008 05:33:54 -0700 (PDT), pereges <Broli00@gmail. com>
        wrote:
        >I just tried to write a small program based on it :
        >
        >
        >#include <stdio.h>
        >#include <stdlib.h>
        >
        >#define PRECISION 2.82e14
        >
        >double drand48(void)
        >{
        > double x = 0;
        > double denom = RAND_MAX + 1;
        > double need;
        >
        > for(need = PRECISION; need 1; need /= (RAND_MAX + 1.))
        {
        > x += rand()/denom;
        > denom *= RAND_MAX + 1. ;
        }
        >
        > return x;
        >
        }
        >
        >
        >int main(void)
        >{
        double x;
        > x = drand48();
        > printf("%f" ,x);
        > x = drand48();
        > printf("%f", x);
        >
        > return 0;
        >
        }
        >
        >
        >But each time I get the same output 0. What could be wrong here ?
        That's not the result I get. Did you cut and paste your code or
        retype it?
        >Does it mean that this will give teh same values over and over again
        >everytime its called ?
        Since your loop always executes the same number of times (four), the
        return value is determined completely by the sequence of values
        returned by rand. If you force rand to return the same sequence (such
        as by calling srand with the same seed before each call to your
        function), then you will get the same value each time. Otherwise not.

        As it is now, since you don't call srand at all, your code will
        produce the same sequence each time the program is run.


        Remove del for email

        Comment

        Working...