00001 #ifndef RANDOM_HPP_
00002 #define RANDOM_HPP_
00003
00004 #include <algorithm>
00005 #include <cstdlib>
00006
00017 class randomint
00018 {
00019 public:
00023 randomint(int low, int high);
00025 int operator()() const;
00026 private:
00027 int const low_;
00028 int const high_;
00029 int const mod_;
00030 int const max_;
00031 };
00032
00033 inline randomint::randomint(int low, int high)
00034 : low_(std::min(low, high)),
00035 high_(std::max(low, high)),
00036 mod_(high_ - low_ + 1),
00037 max_(RAND_MAX - RAND_MAX % mod_)
00038 {}
00039
00040 inline int randomint::operator()()
00041 const
00042 {
00043 int r;
00044 while ((r = std::rand()) > max_)
00045 ;
00046 return r % mod_ + low_;
00047 }
00048
00049 #endif