- Main Page
- Namespaces
- Classes
- Files

# randomint.cpp

Go to the documentation of this file.

    00001
    00005 #include <cstdlib>
    00006 #include <ctime>
    00007 #include <fstream>
    00008 #include <iostream>
    00009 #include <ostream>
    00010
    00011 #include "randomint.hpp"
    00012
    00013 int main(int argc, char** argv)
    00014 {
    00015   if (argc < 3) {
    00016     std::cerr << "usage: " << argv[0] << " LOW HIGH [COUNT]\n";
    00017     return EXIT_FAILURE;
    00018   }
    00019
    00020 #ifdef __LINUX__
    00021   // On GNU/Linux, read the /dev/urandom file to get random bits.
    00022   std::ifstream in("/dev/urandom", std::ios_base::in | std::ios_base::binary);
    00023   if (in) {
    00024     unsigned int seed;
    00025     in.read(reinterpret_cast<char*>(&seed), sizeof(seed));
    00026   }
    00027   else
    00028 #endif
    00029   // On Linux systems where /dev/urandom cannot be read, or on other
    00030   // systems, use the current time as the seed. This is weak for any
    00031   // true need for random integers, but adequate for instructional purposes.
    00032   std::srand(std::time(0));
    00033
    00034   int low = std::atoi(argv[1]);
    00035   int high = std::atoi(argv[2]);
    00036   int count = argc < 4 ? 1 : std::atoi(argv[3]);
    00037
    00038   randomint rng(low, high);
    00039   for (int i = 0; i < count; ++i) {
    00040     std::cout << rng() << '\n';
    00041   }
    00042 }