// Include file: random.h // Date: 2 July 1995 // Programmer: F. D. Lewis // // Generate random numbers by the linear congruential method // // NOTE: to use these routines, you must declare a variable such as: // // long int x; // // to hold the random number #include long int seed() // post: return four digit number based upon time { // declarations long int t, z; int x, x1, x2, x3, x4; long * s; // instructions t = time(s); x = t - (t/10000)*10000; x1 = x/1000; x = x - x1*1000; x2 = x/100; x = x - x2*100; x3 = x/10; x4 = x - x3*10; z = x3*1000 + x1*100 + x4*10 + x2; return z; } // end seed long int random(long int x) // pre: x > 0 // post: return next psuedo-random number by linear congruential method { // declaration long int z; // instructions z = (25173*x + 13849) % 65536; return z; } // end random long int rnd(long int& x, long int range) // pre: x and range > 0 // post: x = next pseudo-random number in sequence // return next pseudo-random number in 1, ..., range // // PROOF: 0 <= x < 65536 // 0 <= x/65536 < 1 // 0 <= (x/65536)*range < range // 0 <= (x/65536)*range <= range - 1 // 1 <= (x/65536)*range + 1 <= range { // declaration long int y; // instructions x = random(x); y = (x*(range)/65536) + 1; return y; } // end rnd