/* Simulation of 1-D mesh grid for CPSC 601.08 */ /* */ /* Written by Carey Williamson, University of Calgary */ /* */ /* Usage: gcc -o grid1 grid1.c */ /* ./grid1 */ #include #include #include /* use man 3 log for the math functions */ #define NUM_POINTS 11 /* Number of WSN nodes to scatter */ #define X_RANGE 1.0 /* Distance in kilometers */ #define X_LINES 4 /* 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, xind; float xcoord[NUM_POINTS]; int nodes[X_LINES]; /* Initialize */ for( i = 0; i < X_LINES; i++ ) { nodes[i] = 0; } srandom(time(NULL)); #ifdef DEBUG printf("Placing %d WSN nodes into %d sub-regions...\n", NUM_POINTS, X_LINES); #endif /* Generate random placement of WSN nodes */ for( i = 0; i < NUM_POINTS; i++ ) { xcoord[i] = Uniform01() * X_RANGE; /* count them up per sub-region */ xind = xcoord[i] / (X_RANGE / X_LINES); nodes[xind]++; #ifdef DEBUG printf("Placed WSN node at %8.6f in region [%d]\n", xcoord[i], xind); #endif } /* Print results */ for( i = 0; i < X_LINES; i++ ) { printf(" %3d ", nodes[i]); printf("\n"); } }