Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
I am porting this program, and it calls a routine drand48, which my library doesnt have. What is it?
I am porting this program, and it calls a routine drand48, which my library doesnt have. What is it?
✍: Guest
drand48 is a Unix System V routine which returns floating point random numbers (presumably with 48 bits of precision) in the half-open interval [0, 1). (Its companion seed routine is srand48; neither is in the C Standard.) It's easy to write a low-precision replacement:
#include <stdlib.h>
double drand48()
{
return rand() / (RAND_MAX + 1.);
}
To more accurately simulate drand48's semantics, you can try to give it closer to 48 bits worth of precision:
#define PRECISION 2.82e14 /* 2**48, rounded up */
double drand48()
{
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;
}
Before using code like this, though, beware that it is numerically suspect, particularly if (as is usually the case) the period of rand is on the order of RAND_MAX. (If you have a longer-period random number generator available, such as BSD random, definitely use it when simulating drand48.)
2015-07-20, 1093👍, 0💬
Popular Posts:
How can I enable session tracking for JSP pages if the browser has disabled cookies? We know that se...
Does .NET support UNICODE and how do you know it supports? Yes .NET definitely supports UNICODE. Try...
Can include files be nested? The answer is yes. Include files can be nested any number of times. As ...
Where are cookies actually stored on the hard disk? This depends on the user's browser and OS. In th...
What Are Named Parameters? - Oracle DBA FAQ - Creating Your Own PL/SQL Procedures and Functions Name...