diff --git a/lemon/random.h b/lemon/random.h --- a/lemon/random.h +++ b/lemon/random.h @@ -66,10 +66,20 @@ #include #include #include +#include #include #include +#ifndef WIN32 +#include +#include +#include +#include +#else +#include +#endif + ///\ingroup misc ///\file ///\brief Mersenne Twister random number generator @@ -526,6 +536,10 @@ public: + ///\name Initialization + /// + /// @{ + /// \brief Default constructor /// /// Constructor with constant seeding. @@ -594,6 +608,73 @@ _random_bits::Initializer::init(core, begin, end); } + /// \brief Seeding from file or from process id and time + /// + /// By default, this function calls the \c seedFromFile() member + /// function with the /dev/urandom file. If it is not success, + /// it uses the \c seedFromTime(). + /// \return Currently always true. + bool seed() { +#ifndef WIN32 + if (seedFromFile("/dev/urandom", 0)) return true; +#endif + if (seedFromTime()) return true; + return false; + } + + /// \brief Seeding from file + /// + /// Seeding the random sequence from file. The linux kernel has two + /// devices, /dev/random and /dev/urandom which + /// could give good seed values for pseudo random generators (The + /// difference between two devices is that the random may + /// block the reading operation while the kernel can give good + /// source of randomness, while the urandom does not + /// block the input, but it could give back bytes with worse + /// entropy). + /// \param file The source file + /// \param offset The offset, from the file read. + /// \return True when the seeding is success. +#ifndef WIN32 + bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0) +#else + bool seedFromFile(const std::string& file = "", int offset = 0) +#endif + { + std::ifstream rs(file.c_str()); + const int size = 4; + Word buf[size]; + if (offset != 0 && !rs.seekg(offset)) return false; + if (!rs.read(reinterpret_cast(buf), sizeof(buf))) return false; + seed(buf, buf + size); + return true; + } + + /// \brief Seding from process id and time + /// + /// Seding from process id and time. This function uses the + /// current process id and the current time for initialize the + /// random sequence. + /// \return Currently always true. + bool seedFromTime() { +#ifndef WIN32 + timeval tv; + gettimeofday(&tv, 0); + seed(getpid() + tv.tv_sec + tv.tv_usec); +#else + FILETIME time; + GetSystemTimeAsFileTime(&time); + seed(GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime); +#endif + return true; + } + + /// @} + + ///\name Uniform distributions + /// + /// @{ + /// \brief Returns a random real number from the range [0, 1) /// /// It returns a random real number from the range [0, 1). The @@ -709,6 +790,8 @@ return bool_producer.convert(core); } + /// @} + ///\name Non-uniform distributions ///