I am writing an Ising model using Monte Carlo algorithm. Why is my random number generator only generating one random number?
The value of a
and b
is not changing as expected. Even though the random number generator is pseudorandom, the numbers are not changing as should be the case.
for(mc=0;mc<=mcs;mc++)
{
srand(time(NULL));
for(i=0;i<N;i++)
{
a=rand()%N;
for(j=0;j<N;j++)
{
b=rand()%N;
for(l=0;l<=3;l++)
{
c= a+(l-1)*(1.0*(1+pow(-1,l))/2.0);
d= b+(l-2)*(1.0*(1-pow(-1,l))/2.0);
if(c==-1)
{
L[c][d]=L[N-2][d];
}
if(c==N)
{
L[c][d]=L[1][d];
}
if(d==-1)
{
L[c][d]=L[c][N-2];
}
if(d==N)
{
L[c][d]=L[c][1];
}
if(L[a][b]*L[c][d]==1)
{
n1= n1+1; //like spins
}
}
n0= 4-n1; //unlike spins
dE= n1-n0;
if(dE<=0)
{
if(L[a][b]== 1)
{
L[a][b]= -1;
}
else
{
L[a][b]= 1;
}
}
else
{
if(dE>0)
{
prob= exp(-dE/(kT));
srand(time(NULL));
r= (float)rand()/RAND_MAX;
if(r<=prob)
{
if(L[a][b]== 1)
{
L[a][b]= -1;
}
else
{
L[a][b]= 1;
}
}
else
{
if(L[a][b]== 1)
{
L[a][b]= 1;
}
else
{
L[a][b]= -1;
}
}
}
}
}
}
}