< previous page page_1096 next page >

Page 1096
Each time this function is called, it returns a random integer in the range 0 through RAND_MAX, a constant defined in stdlib.h. (RAND_MAX is typically the same as INT_MAX.) Because we want our random number to be in the range 1 through 52, we use the conversion formula
Set sizeOfCut = rand() MOD 52+1
or, in C++,
sizeOfCut = rand() % 52 + 1;
Random number generators use an initial seed value from which to start the sequence of random numbers. The C++ library function srand lets you specify an initial seed before calling rand. The prototype for srand is
void srand( unsigned int );
If you do not call srand before the first call to rand, an initial seed of 1 is assumed. As you'll see in the initialization portion of our main function, we input an initial seed value from the user and pass it as a parameter to srand.
FOR count1 going from 1 through numberOfShuffles
  Create empty list halfA
  Create empty list halfB
  Set sizeOfCut = rand() MOD 52 + 1
  Move sizeOfCut cards from deck to halfA
  Move remaining 52-sizeOfCut cards from deck to halfB
  IF sizeOfCut <= 26
    Merge(halfA, halfB)
  ELSE
    Merge(halfB, halfA)

Merge (Inout: shorterList, longerList)
The merge algorithm is much simpler than the one developed in Chapter 14 because a component is taken alternately from each list without regard to the contents of the component. Also, we know the exact length of each list.

 
< previous page page_1096 next page >