Source of rancodig.cpp


  1: // Filename: RANCODIG.CPP
  2: // Purpose:  To provide a random number generator that generates only
  3: //           digits in the range 1..8 for use in Mastermind codes.

  5: #include <time.h>

  7: int RandomCodeDigit()
  8: // Pre:  none
  9: // Post: Return value is an int digit in the range 1..8.
 10: {
 11:     const int MODULUS = 65536;
 12:     const int MULTIPLIER = 25173;
 13:     const int INCREMENT = 13849;

 15:     static int seed = 0;
 16:     static int count = 0;
 17:     ++count;
 18:     if (count == 1)    
 19:         seed = clock();

 21:     double temp;

 23:     seed = ((MULTIPLIER * seed) + INCREMENT) % MODULUS;
 24:     temp = double(seed) / double(MODULUS);
 25:     return int( 8 * temp ) + 1;
 26: }