//  File: main3.C
//
//  Sample program using STL vector templates
//  Read the "spell" dictionary and create a sequence
//  of random words.  This time use lrand48().

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <vector>
#include <string>       // STL string template, like BString
#include <algorithm>    // STL algorithms


//=========================================================================
// Class defined so each call object of this class can be used
// like a random function.
//
class RandomFunction {

public:

   // Default constructor sets the random seed once and only once
   //
   inline RandomFunction() ;

   // overload the () operator
   // returns value < N and >= 0
   //
   int operator()(int N) { return lrand48() % N ;}

private:
   static bool seeded ;
} ;

bool RandomFunction::seeded=false ;

inline RandomFunction::RandomFunction() {
   if (!seeded) {
      srand48(time(NULL)) ;
      seeded = true ;
   }
}

//=========================================================================

main() {
   vector<string> V ;
   ifstream ifile("words") ; // "words" is filename of dictionary
   string str ;
   int i ;
   RandomFunction rand ;     // rand() gives a random number

   // Play with rand
   //
   cout << "Some random numbers: " ;
   for (i = 0 ; i < 10 ; i++) {
      cout << rand(1000) << "  " ;
   }
   cout << endl ;

   // Get all strings from ifile
   //
   while (1) {
      ifile >> str ;
      if (ifile.eof()) break ;

      V.push_back(str) ;
   }

   srand48(time(NULL)) ;    // set random seed

   random_shuffle(V.begin(), V.end(), rand) ;

   cout << "10 random words" << endl ;
   for (i = 0 ; i < 10 ; i++) {
      cout << V.back() << endl ;
      V.pop_back() ; // remove last element
   }
}
