# Good Random Number Generator

This is a discussion on Good Random Number Generator within the C Programming forums, part of the General Programming Boards category; Hi, Im using the Monte Carlo method to get an estimate of Pi. I generate numbers between 0.0 and 1.0, ...

1. ## 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?

2. >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 ?

3. 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().

4. 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

5. Take a look at the Mersenne Twister pseudo-random number generator.