using ran2 with changes


JackSiu
03-27-2004, 11:38 AM
Hi, thanks for reading this!

I am currently trying to use the 'ran2' method described in the NR for C++ book with some changes to types in the code. The changes are:

-- instead of using type DP, i used double instead which is already using 64 bit register in MS Visual Studio .Net.

--instead of
'static Vec_INT' iv(NTAB)',
i've used a standard declaration for a single dimension int array:
'static int iv[NTAB];'


so the code now looks like:

double ran2(int idum)
{
const int IM1=2147483563, IM2=2147483399;
const int IA1=40014 ,IA2=40692, IQ1=53668; const int IQ2=52774;
const int IR1=12211,IR2=3791,NTAB=32,IMM1=IM1-1;
const int NDIV=1+IMM1/NTAB;
const double EPS=3.0e-16,RNMX=1.0-EPS;
const double AM=1.0/double(IM1);
static int idum2=123456789,iy=0;
static int iv[NTAB];
int j,k;
double temp;

if (idum <= 0)
{
idum = (idum == 0 ? 1 : -idum);
idum2=idum;
for (j = NTAB + 7; j >= 0; j--)
{
k = idum / IQ1;
idum = IA1*(idum-k*IQ1)-k*IR1;
if (idum < 0) idum += IM1;
if (j < NTAB) iv[j] = idum;
}
iy = iv[0];
}
k = idum / IQ1;
idum = IA1 * (idum - k * IQ1) - k * IR1;
if (idum < 0) idum += IM1;
k = idum2 /IQ2;
idum2 = IA2 * (idum2 - k * IQ2) - k * IR2;
if (idum2 < 0) idum += IM2;
j = iy / NDIV;
iy = iv[j] - idum2;
iv[j] = idum;
if (iy < 1) iy += IMM1;
if ((temp = AM * iy) > RNMX) return RNMX;
else return temp;
}


Which from as far as my understanding is the same to the method used in the code and shouldn't have done any changes to the results.

I then invoked ran2 with the following code in my main function:

for (int i = 0; i < 100; i++)
{
cout << ran2(-384501) << endl;
}

which displays the numbers generated in a console line by line.

However, the returned result is 0.658422 repeated 100 times. Which is completely not random. Similar situations happen when I use different seeds. There are occations when a random sequence is generated, however, the same sequence is generated using different seeds.

I have looked through the entire forum and cannot find a solution to my problem. Is there any procedure I done wrong in calling the ran2 in the main function of the program? or is there any specific ways to use the ran2 generator which is not mentioned in the book? or otherwise, please tell me.

THank You again for your time.

Yours Sincerely,
Chi Ming Siu
Final Year undergraduate doing his Final Year Project

JackSiu
03-27-2004, 12:43 PM
The following is the random sequence generated with different seeds resulting in a non-repeating sequence:

0.655416
0.200995
0.893622
0.281887
0.525
0.314127
0.444616
0.299474
etc

And I just found out that it is only the first 8 random numbers generated using ran2 are the same in all different seeds, meaning random numbers from the 9th one onwards are all different.

Does this imply the random numbers generated after the 8th execution of ran2 is then random?

Thank You!

Yours Sincerely,
Chi Ming Siu

JackSiu
03-27-2004, 12:53 PM
I believe I got the same results as bgbg in a forum discussed below mine in the list. However, I already used negative seeds. It hasn't solve the problem!

KZoli
03-22-2005, 06:12 AM
As I can seen, in the original code the function ran2 obtains a reference variable, so it can change the value of it. But if you give a constant value as an arqument it will calculate the same number over and over again.

ydw
03-31-2007, 01:02 AM
I then invoked ran2 with the following code in my main function:

for (int i = 0; i < 100; i++)
{
cout << ran2(-384501) << endl;
}

which displays the numbers generated in a console line by line.

However, the returned result is 0.658422 repeated 100 times.

You are calling ran2 100 times with the same value for idum each time, so you will of course get the same number 100 times. You need to allow idum to change. Try

int jdum=-384501;
for (int i = 0; i < 100; i++)
{
cout << ran2(jdum) << endl;
}