# include # include int iran ( int a, int b, int *seed ) // // Purpose: // // IRAN returns a scaled pseudorandom integer between A and B. // // Parameters: // // Input, int A, B, the limits of the interval. // // Input/output, int *SEED, the "seed" value, which should NOT be 0. // On output, SEED has been updated. // // Output, int IRAN, a number between A and B. // { int i4_huge = 2147483647; int k; float value; // // Check SEED. // if ( *seed == 0 ) { printf ( "\n" ); printf ( "IRAN - Fatal error!\n" ); printf ( " Input value of SEED = 0.\n" ); exit ( 1 ); } // // Update SEED. // k = *seed / 127773; *seed = 16807 * ( *seed - k * 127773 ) - k * 2836; if ( *seed < 0 ) { *seed = *seed + i4_huge; } // // Use the MOD function. // if ( b < a ) { value = b + ( *seed % ( a - b ) ); } else if ( b == a ) { value = a; } else { value = a + ( *seed % ( b - a ) ); } return value; }