/*************************    Program 2.14   ****************************/
/*                                                                      */
/************************************************************************/
/* Please Note:                                                         */
/*                                                                      */
/* (1) This computer program is written by Tao Pang in conjunction with */
/*     his book, "An Introduction to Computational Physics," published  */
/*     by Cambridge University Press in 1997.                           */
/*                                                                      */
/* (2) No warranties, express or implied, are made for this program.    */
/*                                                                      */
/************************************************************************/

#define NMAX 101

void percolation (l,n,p)
/* Subroutine to generate a square percolation lattice.  l=1
   corresponds a occupied site and l=0 corresponds an empty
   site.  Copyright (c) Tao Pang 1997. */
int n;
double p;
int l[NMAX][NMAX];
{
int i,j;
double r;
double ranf();

for (i = 0; i < n; ++i)
  {
  for (j = 0; j < n; ++j)
    {
    r = ranf();
    if (r < p)
      {
      l[i][j] = 1;
      }
    else
      {
      l[i][j] = 0;
      }
    }
  }
}

double ranf()
/* Uniform random number generator x(n+1)= a*x(n) mod c
   with a = pow(7,5) and c = pow(2,31)-1.
   Copyright (c) Tao Pang 1997. */
{
const int ia=16807,ic=2147483647,iq=127773,ir=2836;
int il,ih,it;
double rc;
extern int iseed;
ih = iseed/iq;
il = iseed%iq;
it = ia*il-ir*ih;
if (it > 0)
  {
  iseed = it;
  }
else
  {
iseed = ic+it;
  }
rc = ic;
return iseed/rc;
}
