0

For generating random doubles, there is drand48, but

These functions are declared obsolete by SVID 3, which states that rand(3) should be used instead.

How do I construct my random double with drand48? On a side note, the random float?

Simply concatenating two int from rand() and casting may lead to NaN, which is not acceptable. I'd like to avoid using only 32 random bits for a double.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Reactormonk
  • 21,472
  • 14
  • 74
  • 123
  • 3
    [This might help.](http://stackoverflow.com/questions/1340729/how-do-you-generate-a-random-double-uniformly-distributed-between-0-and-1-from-c) –  Sep 05 '12 at 20:59

1 Answers1

4

This citation of an obsolete standard in the man pages for linux is unfortunate. If I see correctly SVID 3 was published 1986, and obsoleted itself since long. POSIX has this family of functions and there are no plans to phase them out.

But if you are at it, consider to use erand48 instead of drand48. It has the advantage that you provide it with a seed state yourself. By that it is reentrant, generally behaves better if you use it in a threaded environment (and initialize the seeds for threads differently) or is deterministic if you need it for reproducible simulations.

The only little thing is that you have to have in mind is that it only gives you 48 bits of pseudo-randomness. Usual double arithmetic is with 52 bit, so be careful that you don't use the lowest 4 bits, they are no good.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177