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