Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Obtaining double precision random number?

Thread Tools

Obtaining double precision random number?

Ronny Mandal
Posts: n/a
Is there a function that will do this task properly?



Ronny Mandal

Reply With Quote
Posts: n/a
"Ronny Mandal" writes:

> Is there a function that will do this task properly?

Not in the standard libraries. If you want numbers in the range 0..1.0
there is a lot of guidance on the Web as to how to produce such numbers from
the integer returned by rand().

Reply With Quote
Eric Sosman
Posts: n/a
Ronny Mandal wrote:
> Is there a function that will do this task properly?

("This task" is to obtain a "double precision random number."
For future reference, it's a good idea to put your question in
the body of the message, even if it's the same as the Subject.)

The C library has no function to do this. However, it provides
the tools you need to do it yourself, in at least two ways:

/* Method 1 (simple and sloppy) */
#include <stdlib.h>
double r = rand() / (RAND_MAX + 1.0);

This sets `r' to a `double' value between zero (inclusive) and
one (exclusive, almost certainly). However, even though `r' is a
`double' it is probably not "double precision." rand() can produce
at most RAND_MAX+1 different values and RAND_MAX can be as small as
32767, so `r' could have as little as fifteen bits of "precision."
To get finer "grain" in the result you need to combine several rand()

/* Method 2 (more involved, more "precise") */
#include <stdlib.h>
#include <float.h>
double r = 0.0;
double s = 1.0;
do {
s /= RAND_MAX + 1.0;
r += rand() * s;
} while (s > DBL_EPSILON);

Roughly speaking, this method builds `r' as a fraction in the
base RAND_MAX+1, with each rand() contributing a new "digit." The
loop continues tacking on "digits" until their significance becomes
too small to matter (DBL_EPSILON is the difference between 1.0 and
the smallest `double' value larger than 1.0; it is the "grain size"
of `double').

HOWEVER, method 2 is not suitable for "high-precision" work
because the guarantees on the quality of rand() itself are too weak.
The method forms `r' from N successive rand() values (two to four
on many machines), and many pseudo-random generators suffer from
accuracy problems when viewed as sources of N-tuples for N>1 (Google
"spectral test" for more information). If you really need "double
precision," you should probably use something other than the generic
rand() as a source of random bits;
is a good place to start searching.

Eric Sosman Removed)lid
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Bug in double precision computing? Daniel Geisenhoff ASP .Net 1 07-29-2004 01:58 PM
Re: What I meaned with "random precision"! Tom St Denis C Programming 7 06-27-2003 06:22 PM