# Good Random Number Generator

• 11-17-2004
MethodMan
Good Random Number Generator
Hi,

Im using the Monte Carlo method to get an estimate of Pi.

I generate numbers between 0.0 and 1.0, using the following function (similar to the one in the FAQ)

Code:

```double generate_rand(double min, double max) {   static int Init = 0;   double rc;     if (Init == 0)   {     srand(time(NULL));     Init = 1;   }   rc =  (rand() / (RAND_MAX + 1.0) * (max - min) + min);   return rc; }```
The estimation of pi depends on the quality of the random number generator, I was wondering if there is any way to make my function generate 'better' random numbers?
• 11-17-2004
Dave_Sinkula
>I was wondering if there is any way to make my function generate 'better' random numbers?

http://www.stanford.edu/~blp/writings/clc/random.html ?
• 11-17-2004
okinrus
You could use a 64bit random number generator. You can build one by using 64-bit numbers and bit-shifting 24-bit random digits from rand().
• 11-17-2004
xErath
You could combine rand with XORs. As more, as better.
Code:

```int cnt; int res = rand(); for(cnt=rand()&0x07;cnt>=0;cnt--)     res = res ^ rand();```
But wouldn't be any good algorithm... just to clean your hand from time to time
• 11-18-2004
laserlight
Take a look at the Mersenne Twister pseudo-random number generator.