COIN-OR::LEMON - Graph Library

Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/random.h

    r116 r178  
    6767#include <vector>
    6868#include <limits>
     69#include <fstream>
    6970
    7071#include <lemon/math.h>
    7172#include <lemon/dim2.h>
     73
     74#ifndef WIN32
     75#include <sys/time.h>
     76#include <ctime>
     77#include <sys/types.h>
     78#include <unistd.h>
     79#else
     80#include <windows.h>
     81#endif
    7282
    7383///\ingroup misc
     
    527537  public:
    528538
     539    ///\name Initialization
     540    ///
     541    /// @{
     542
     543    ///\name Initialization
     544    ///
     545    /// @{
     546
    529547    /// \brief Default constructor
    530548    ///
     
    595613    }
    596614
     615    /// \brief Seeding from file or from process id and time
     616    ///
     617    /// By default, this function calls the \c seedFromFile() member
     618    /// function with the <tt>/dev/urandom</tt> file. If it does not success,
     619    /// it uses the \c seedFromTime().
     620    /// \return Currently always true.
     621    bool seed() {
     622#ifndef WIN32
     623      if (seedFromFile("/dev/urandom", 0)) return true;
     624#endif
     625      if (seedFromTime()) return true;
     626      return false;
     627    }
     628   
     629    /// \brief Seeding from file
     630    ///
     631    /// Seeding the random sequence from file. The linux kernel has two
     632    /// devices, <tt>/dev/random</tt> and <tt>/dev/urandom</tt> which
     633    /// could give good seed values for pseudo random generators (The
     634    /// difference between two devices is that the <tt>random</tt> may
     635    /// block the reading operation while the kernel can give good
     636    /// source of randomness, while the <tt>urandom</tt> does not
     637    /// block the input, but it could give back bytes with worse
     638    /// entropy).
     639    /// \param file The source file
     640    /// \param offset The offset, from the file read.
     641    /// \return True when the seeding successes.
     642#ifndef WIN32
     643    bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0)
     644#else
     645    bool seedFromFile(const std::string& file = "", int offset = 0)
     646#endif
     647    {
     648      std::ifstream rs(file.c_str());
     649      const int size = 4;
     650      Word buf[size];
     651      if (offset != 0 && !rs.seekg(offset)) return false;
     652      if (!rs.read(reinterpret_cast<char*>(buf), sizeof(buf))) return false;
     653      seed(buf, buf + size);
     654      return true;
     655    }
     656
     657    /// \brief Seding from process id and time
     658    ///
     659    /// Seding from process id and time. This function uses the
     660    /// current process id and the current time for initialize the
     661    /// random sequence.
     662    /// \return Currently always true.
     663    bool seedFromTime() {       
     664#ifndef WIN32
     665      timeval tv;
     666      gettimeofday(&tv, 0);
     667      seed(getpid() + tv.tv_sec + tv.tv_usec);
     668#else
     669      FILETIME time;
     670      GetSystemTimeAsFileTime(&time);
     671      seed(GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime);
     672#endif
     673      return true;
     674    }
     675
     676    /// @}
     677
     678    ///\name Uniform distributions
     679    ///
     680    /// @{
     681
    597682    /// \brief Returns a random real number from the range [0, 1)
    598683    ///
     
    623708      return real<Number>() * (b - a) + a;
    624709    }
     710
     711    /// @}
     712
     713    ///\name Uniform distributions
     714    ///
     715    /// @{
    625716
    626717    /// \brief Returns a random real number from the range [0, 1)
     
    680771      return _random_bits::IntConversion<Number, Word>::convert(core);
    681772    }
     773
     774    /// @}
    682775
    683776    unsigned int uinteger() {
     
    709802      return bool_producer.convert(core);
    710803    }
     804
     805    /// @}
    711806
    712807    ///\name Non-uniform distributions
Note: See TracChangeset for help on using the changeset viewer.