/*************************************************************** *** Program to generate data files for individual plant *** growth model. Simulates planting of 21 9 cm spaced *** rows and a similar number of perpendicular columns. *** Plants are placed in rows first with even spacing and *** then a normal random deviate is applied to that position *** within a row/column. Input is total density for 180 square *** cm. This is split evenly between row and columns ****************************************************************/ #include #include #include void main(int argc, char *argv[]) { float ran1(); int nper_row, i; float density; int x, y, pos_x, pos_y; FILE *outfile; char data_file[20] = "den_"; static long idum; idum = (0 - time(NULL)); density = atof(argv[1]); printf("density is: %f\n", density); nper_row = (int)(density/42.0); printf("nper_row is: %d\n", nper_row); strcat(data_file, argv[1]); strcat(data_file, ".dat"); if ((outfile=fopen(data_file, "w")) == NULL) { printf("Can't open input file %s\n", data_file); exit(1); } for(y = 0; y <= 20; ++y){ for(i = 1; i <= nper_row; ++i){ pos_x = (int)((ran1(&idum)*180) + 275); pos_y = (int)((y*9) + 120); fprintf(outfile, "%3d %3d\n", pos_x, pos_y); } } for(x = 0; x <= 20; ++x){ for(i = 1; i <= nper_row; ++i){ pos_y = (int)((ran1(&idum)*180) + 120); pos_x = (int)((x*9) + 275); fprintf(outfile, "%3d %3d\n", pos_x, pos_y); } } fclose(outfile); } /****************************************************************** This is the random number generator ran1() from "Numerical Recipes in C" ******************************************************************/ #define IA 16807 #define IM 2147483647 #define AM (1.0/IM) #define IQ 127773 #define IR 2836 #define NTAB 32 #define NDIV (1+(IM-1)/NTAB) #define EPS 1.2e-7 #define RNMX (1.0-EPS) float ran1(long *idum) /* Minimum random number generator of Park an Miller with Bays-Durham shuffle and added safeguards. Returns uniform random deviate between 0.0 and 1.0 (exclusive of the endpoint values). Call with idum a negative integer to initialize; thereafter, do not alter idum between successive deviates in a sequence. RNMX should approximate the largest floating value that is less than 1. */ { int j; long k; static long iy=0; static long iv[NTAB]; float temp; if(*idum <= 0 || !iy){ if(-(*idum) < 1) *idum=1; else *idum = -(*idum); for(j = NTAB+7;j >= 0;j--){ k = (*idum)/IQ; *idum = IA*(*idum - k*IQ) - IR*k; if(*idum < 0) *idum += IM; if(j < NTAB) iv[j] = *idum; } iy = iv[0]; } k = (*idum)/IQ; /* Start here when not initializing */ *idum = IA*(*idum - k*IQ) - IR*k; /* Compute idum = (IA*idum) % IM without overflows by Schrage's method. */ if(*idum < 0) *idum += IM; j = iy/NDIV; /* Will be in the range 0..NTAB-1. */ iy = iv[j]; /* Output previously stored value and refill the shuffle table. */ iv[j] = *idum; if((temp = AM*iy) > RNMX) return RNMX; /* Because users don't expect endpoint values. */ else return temp; } /* end ran1 */