-1

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;

                        }
                    }

                }
            }
        }
    }
}
lebelinoz
  • 4,890
  • 10
  • 33
  • 56

1 Answers1

3

Move srand(time(NULL)); before the for loop. The way it is now, you will get the same numbers for each iteration of the for (in case tha calls to srand are done at the same second, as noted by StoryTeller and chux). Seed just once before your loop and the this problem should be resolved.

Gnqz
  • 3,292
  • 3
  • 25
  • 35
  • 1
    Some polish: You will get the same numbers so long as the looping occurs in less then a second (true on most machines), so `time(NULL)` just keeps returning the same value for the seed. – StoryTeller - Unslander Monica May 08 '17 at 12:49
  • @StoryTeller Some more polish. "get the same numbers so long as the looping occurs within the same second (time step)" With as little as 2 interactions, the _second_ may change. – chux - Reinstate Monica May 08 '17 at 16:11