//: C06:Generators.h // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // Different ways to fill sequences. #ifndef GENERATORS_H #define GENERATORS_H #include #include #include // A generator that can skip over numbers: class SkipGen { int i; int skp; public: SkipGen(int start = 0, int skip = 1) : i(start), skp(skip) {} int operator()() { int r = i; i += skp; return r; } }; // Generate unique random numbers from 0 to mod: class URandGen { std::set used; int limit; public: URandGen(int lim) : limit(lim) {} int operator()() { while(true) { int i = int(std::rand()) % limit; if(used.find(i) == used.end()) { used.insert(i); return i; } } } }; // Produces random characters: class CharGen { static const char* source; static const int len; public: char operator()() { return source[std::rand() % len]; } }; #endif // GENERATORS_H ///:~