Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drand48 on Windows

I am looking for an equivalent to drand48 on Windows. To all who do not know, the following is not equivalent:

(double)rand()/RAND_MAX;

Firstly, rand returns values including RAND_MAX Secondly, on Windows RAND_MAX=32767 which is a too short period for my application.

My purpose is to generate noise for a simulation. It is desirable to use a pseudo-random generator with the same period as drand48.

like image 893
user877329 Avatar asked Jun 16 '12 18:06

user877329


2 Answers

Firstly, note that you appear to be confusing the resolution with the period. On Windows, rand will return values from 0 to 32767, but this does not mean that the same values will repeat every 32768 calls. So rand should be perfectly adequate unless you need more than 16 bits of resolution. (The resolution and the period are the same in drand48, but not in all pseudorandom number generators.)

If you do not need the exact behaviour of drand48, rand_s would be the simplest option. It has a 32-bit resolution, less than drand48 but enough for most purposes. It generates a cryptographically secure random number, so there is no fixed period. One possible issue is that it will be much slower than drand48.

If you want the same behaviour of drand48, the algorithm is documented and should be easy to re-implement, or you could use the source code from FreeBSD (link to source code browser on http://fxr.watson.org/).

like image 62
Harry Johnston Avatar answered Oct 11 '22 02:10

Harry Johnston


7 years late to the party. Sorry.

Gnu Scientific Library is a good solution to this problem. The library employs several high-quality generating algorithms.

https://www.gnu.org/software/gsl/doc/html/rng.html

like image 31
Applegrafter Avatar answered Oct 11 '22 02:10

Applegrafter