//==================================================== file = shuffle.c ===== //= Program to shuffle a series of size N = //= - Implements a paired shuffle (for M * N shuffles) = //=========================================================================== //= Notes: = //= 1) Input from input file "in.dat" to stdin (see example below) = //= * Comments are bounded by "&" characters at the beginning and = //= end of the comment block = //= 2) Outputs the series shuffled to stdout = //= 3) Number of complete shuffles, M, is set in #define section = //=-------------------------------------------------------------------------= //= Example "in.dat" file: = //= = //= & Sample series of data which can be integers or reals. = //= There are 10 values in this file. & = //= 1 = //= 2 = //= 3 = //= 4 = //= 5 = //= 6 = //= 7 = //= 8 = //= 9 = //= 10 = //=-------------------------------------------------------------------------= //= Example output (for above "in.dat" and M = 5): = //= = //= 7.000000 = //= 3.000000 = //= 5.000000 = //= 1.000000 = //= 4.000000 = //= 2.000000 = //= 9.000000 = //= 8.000000 = //= 10.000000 = //= 6.00000 = //=-------------------------------------------------------------------------= //= Build: bcc32 shuffle.c = //=-------------------------------------------------------------------------= //= Execute: shuffle < in.dat = //=-------------------------------------------------------------------------= //= Author: Kenneth J. Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (06/14/03) - Genesis (from shuffle1.c) = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() and feof() #include // Needed for exit() and atof() #include // Needed for strcmp() #include // Needed for ceil() //----- Defines ------------------------------------------------------------- #define MAX_SIZE 2000000 // Maximum size of time series data array #define M 100 // Block size for external shuffle //----- Globals ------------------------------------------------------------- double X[MAX_SIZE]; // Time series read from "in.dat" int N; // Number of values in "in.dat" //----- Function prototypes ------------------------------------------------- void load_X_array(void); // Load X array void shuffle(void); // Shuffle the X array double rand_val(int seed); // Jain's RNG //=========================================================================== //= Main program = //=========================================================================== void main(void) { int i; // Loop counter // Load the series X load_X_array(); // Do the shuffle shuffle(); // Output the shuffled series for (i=0; i= MAX_SIZE) { printf("*** ERROR - greater than %ld data values \n", MAX_SIZE); exit(1); } } // End-of-file escape end: return; } //=========================================================================== //= Function to shuffle series X = //=========================================================================== void shuffle(void) { int from_index; // From index int to_index; // To index double temp; // Temporary value int i, j; // Loop counters // Seed the RNG rand_val(1); // Do a complete shuffle M times for (i=0; i 0) { x = seed; return(0.0); } // RNG using integer arithmetic x_div_q = x / q; x_mod_q = x % q; x_new = (a * x_mod_q) - (r * x_div_q); if (x_new > 0) x = x_new; else x = x_new + m; // Return a random value between 0.0 and 1.0 return((double) x / m); }