473,757 Members | 6,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Obtaining double precision random number?

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

Thanks

Ronny Mandal

Nov 14 '05 #1
2 5453
"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().
Nov 14 '05 #2
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()
values:

/* 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; http://random.mat.sbg.ac.at/news
is a good place to start searching.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
22629
by: f | last post by:
I have this double sum, a, b, c; sum = a + b + c; printf("%.20f = %.20f, %.20f, %.20f", sum, a, b, c); I found that the debug version and release version of the same code give me different result. I am using VC++ 6.0. In debug version, the print out is:
6
30163
by: J | last post by:
Would anyone know if there a type tag to format a double? I have f for floating point, but cannot find one for double.
5
6090
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this purpose, that isn't enough. I was reading a book about some of the financial scandals of the 1990s called "Inventing Money: The Story of Long-Term Capital Management and the Legends Behind it" by Nicholas Dunbar. On page 95, he mentions that...
3
23755
by: Madan | last post by:
Hi all, I had problem regarding float/double arithmetic only with + and - operations, which gives inaccurate precisions. I would like to know how the arithmetic operations are internally handled by C# or they are hardware (processor) dependent. Basic addition operation errors, for ex: 3.6 - 2.4 = 1.19999999999 or 1.20000000003 There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please reply me how these operations are...
10
18769
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
60
7220
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
11
1773
by: rshepard | last post by:
I start with a list of tuples retrieved from a database table. These tuples are extracted and put into individual lists. So I have lists that look like this: . When I concatenate lists, I end up with a list of lists that looks like this: . ]. Then, I average the column values so I end up with a single list, but with two brackets on each end, for example, ]. Unfortunately, when I try to use that last list in a NumPy function, I'm told...
29
2966
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any bug , i am still c++ beginner
2
5118
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double amount = 126.60; double interestRate = .075; double interest = amount * interestRate;
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.