#include <stdio.h>

double real_random(int done);

void grow_polymer( int *p_grid, int dim, int ix, int iy, int len)
{
int iii, id, ixnew, iynew;
int *p_point;

/*** grow polymer chain ***/
    for (iii=1; iii<len; iii++)
      {
        id=1;
        ixnew = ix; iynew = iy;
        if (real_random(1) < 0.5) id=-1;

        if (real_random(1) < 0.5)
          {
             ixnew += id;
             if (ixnew > dim-1) ixnew = 0;
             if (ixnew < 0) ixnew += dim;
          }
        else
          {
             iynew += id;
             if (iynew > dim-1) iynew = 0;
             if (iynew < 0) iynew = iynew+dim;
          }

/*** avoid existing chain ***/
        p_point= p_grid + ixnew*dim + iynew;
        if ( *p_point==1) iii--;
        else
          {
             ix = ixnew; iy = iynew;
        /*     printf("next monomer : %d %d\n", ix,iy); */
             p_point= p_grid + ix*dim + iy;
             *p_point=1;
          }
      }

return;
}
