/* Simulation of 2-D mesh grid for CPSC 601.08 */ /* */ /* Written by Carey Williamson, University of Calgary */ /* */ /* Usage: gcc -o grid2 grid2.c */ /* ./grid2 */ #include #include #include /* use man 3 log for the math functions */ #define NUM_POINTS 1000 /* Number of WSN nodes to scatter */ #define X_RANGE 1.0 /* Distance in kilometers */ #define Y_RANGE 1.0 /* Distance in kilometers */ #define X_LINES 10 #define Y_LINES 10 /* Debugging output */ /* #define DEBUG 1 */ /***********************************************************************/ /* RANDOM NUMBER GENERATION STUFF */ /***********************************************************************/ /* Parameters for random number generation. */ #define MAX_INT 2147483647 /* Maximum positive integer 2^31 - 1 */ /* Generate a random floating point value uniformly distributed in [0,1] */ float Uniform01() { float randnum; /* get a random positive integer from random() */ randnum = (float) 1.0 * random(); /* divide by max int to get something in 0..1 */ randnum = (float) randnum / (1.0 * MAX_INT); return( randnum ); } /***********************************************************************/ /* MAIN PROGRAM */ /***********************************************************************/ int main() { int i, j, xind, yind; float xcoord[NUM_POINTS]; float ycoord[NUM_POINTS]; int nodes[X_LINES][Y_LINES]; /* Initialize */ for( i = 0; i < X_LINES; i++ ) { for( j = 0; j < Y_LINES; j++ ) { nodes[i][j] = 0; } } srandom(time(NULL)); #ifdef DEBUG printf("Placing %d WSN nodes into %d x %d sub-regions...\n", NUM_POINTS, X_LINES, Y_LINES); #endif /* Generate random placement of WSN nodes */ for( i = 0; i < NUM_POINTS; i++ ) { xcoord[i] = Uniform01() * X_RANGE; ycoord[i] = Uniform01() * Y_RANGE; /* count them up per sub-region */ xind = xcoord[i] / (X_RANGE / X_LINES); yind = ycoord[i] / (Y_RANGE / Y_LINES); nodes[xind][yind]++; #ifdef DEBUG printf("Placed WSN node at (%8.6f,%8.6f) in region [%d][%d]\n", xcoord[i], ycoord[i], xind, yind); #endif } /* Print results */ for( i = 0; i < X_LINES; i++ ) { for( j = 0; j < Y_LINES; j++ ) { printf(" %3d ", nodes[i][j]); printf("\n"); } } }