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
http://www.cs.virginia.edu/~lcc-win32
.



Relevant Pages

  • Re: new order doubt
    ... Warning test2.c: 8 no type specified. ... Defaulting to int ... Even Microsoft has problems with this obscure rules. ... jacob at jacob point remcomp point fr ...
    (comp.lang.c)
  • Re: strtod strange behaviour
    ... int main ... char *p = NULL; ... printf("%s converted is %g\n", szValue, a); ... jacob at jacob point remcomp point fr ...
    (comp.lang.c)
  • Re: Gauss Hypergeometric Function 2F1(a,b;c;x)
    ... I have programmed the Gauss Hypergeometric Function 2F1for all possible real arguments and would like to check it with ... some benchmarks or a particularly challenging set of parameters. ... Results should be in extended or double precision format. ... jacob at jacob point remcomp point fr ...
    (sci.math.num-analysis)
  • Re: Multi precision floating point
    ... to go beyond single precision, ... A whole three minutes to advertise your compiler, jacob? ... If, on the other hand, you want to write multiple-precision floating-point as a learning exercise, or you forsee ever needing to use your code on an implementation other than jacob's, or you simply want to keep the choice of switching vendors, then it is perfectly possible to write multiple-precision floating-point in Standard C. ... That is because those are libraries, whereas qfloat is a language extension, which adds to the C syntax - something libraries cannot do. ...
    (comp.lang.c)
  • Re: size_t problems
    ... strlen's result to an int is clearly wrong; strlen yields size_t, ... but that does not make him a troll. ... Now, I sometimes disagree with Jacob, but I think he makes an honest ...
    (comp.lang.c)