lemon/random.h
changeset 1142 2f479109a71d
parent 1132 0998f70d0b2d
parent 1134 f70f688d9ef9
child 1163 db1d342a1087
equal deleted inserted replaced
29:1766581bd7e4 31:44bc2ed4a89f
    60  */
    60  */
    61 
    61 
    62 #ifndef LEMON_RANDOM_H
    62 #ifndef LEMON_RANDOM_H
    63 #define LEMON_RANDOM_H
    63 #define LEMON_RANDOM_H
    64 
    64 
       
    65 #include <lemon/config.h>
       
    66 
    65 #include <algorithm>
    67 #include <algorithm>
    66 #include <iterator>
    68 #include <iterator>
    67 #include <vector>
    69 #include <vector>
    68 #include <limits>
    70 #include <limits>
    69 #include <fstream>
    71 #include <fstream>
    70 
    72 
    71 #include <lemon/math.h>
    73 #include <lemon/math.h>
    72 #include <lemon/dim2.h>
    74 #include <lemon/dim2.h>
    73 
    75 
    74 #ifndef WIN32
    76 #ifndef LEMON_WIN32
    75 #include <sys/time.h>
    77 #include <sys/time.h>
    76 #include <ctime>
    78 #include <ctime>
    77 #include <sys/types.h>
    79 #include <sys/types.h>
    78 #include <unistd.h>
    80 #include <unistd.h>
    79 #else
    81 #else
   603     /// By default, this function calls the \c seedFromFile() member
   605     /// By default, this function calls the \c seedFromFile() member
   604     /// function with the <tt>/dev/urandom</tt> file. If it does not success,
   606     /// function with the <tt>/dev/urandom</tt> file. If it does not success,
   605     /// it uses the \c seedFromTime().
   607     /// it uses the \c seedFromTime().
   606     /// \return Currently always \c true.
   608     /// \return Currently always \c true.
   607     bool seed() {
   609     bool seed() {
   608 #ifndef WIN32
   610 #ifndef LEMON_WIN32
   609       if (seedFromFile("/dev/urandom", 0)) return true;
   611       if (seedFromFile("/dev/urandom", 0)) return true;
   610 #endif
   612 #endif
   611       if (seedFromTime()) return true;
   613       if (seedFromTime()) return true;
   612       return false;
   614       return false;
   613     }
   615     }
   623     /// block the input, but it could give back bytes with worse
   625     /// block the input, but it could give back bytes with worse
   624     /// entropy).
   626     /// entropy).
   625     /// \param file The source file
   627     /// \param file The source file
   626     /// \param offset The offset, from the file read.
   628     /// \param offset The offset, from the file read.
   627     /// \return \c true when the seeding successes.
   629     /// \return \c true when the seeding successes.
   628 #ifndef WIN32
   630 #ifndef LEMON_WIN32
   629     bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0)
   631     bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0)
   630 #else
   632 #else
   631     bool seedFromFile(const std::string& file = "", int offset = 0)
   633     bool seedFromFile(const std::string& file = "", int offset = 0)
   632 #endif
   634 #endif
   633     {
   635     {
   645     /// Seding from process id and time. This function uses the
   647     /// Seding from process id and time. This function uses the
   646     /// current process id and the current time for initialize the
   648     /// current process id and the current time for initialize the
   647     /// random sequence.
   649     /// random sequence.
   648     /// \return Currently always \c true.
   650     /// \return Currently always \c true.
   649     bool seedFromTime() {
   651     bool seedFromTime() {
   650 #ifndef WIN32
   652 #ifndef LEMON_WIN32
   651       timeval tv;
   653       timeval tv;
   652       gettimeofday(&tv, 0);
   654       gettimeofday(&tv, 0);
   653       seed(getpid() + tv.tv_sec + tv.tv_usec);
   655       seed(getpid() + tv.tv_sec + tv.tv_usec);
   654 #else
   656 #else
   655       seed(bits::getWinRndSeed());
   657       seed(bits::getWinRndSeed());