COIN-OR::LEMON - Graph Library

Ticket #19: b685e12e08c0.patch

File b685e12e08c0.patch, 3.5 KB (added by Balazs Dezso, 16 years ago)
  • lemon/random.h

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1213865962 -7200
    # Node ID b685e12e08c0435fb2328d0906888f9c278f8b1d
    # Parent  47b69d4b0759c4957dc380cde2aa28b8aaa5e0e4
    Seeding from file source or from pid and time (ticket #19)
    
    diff -r 47b69d4b0759 -r b685e12e08c0 lemon/random.h
    a b  
    6666#include <iterator>
    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
    7484///\file
     
    526536
    527537  public:
    528538
     539    ///\name Initialization
     540    ///
     541    /// @{
     542
    529543    /// \brief Default constructor
    530544    ///
    531545    /// Constructor with constant seeding.
     
    593607      typedef typename std::iterator_traits<Iterator>::value_type Number;
    594608      _random_bits::Initializer<Number, Word>::init(core, begin, end);
    595609    }
     610
     611    /// \brief Seeding from file or from process id and time
     612    ///
     613    /// By default, this function calls the \c seedFromFile() member
     614    /// function with the <tt>/dev/urandom</tt> file. If it is not success,
     615    /// it uses the \c seedFromTime().
     616    /// \return Currently always true.
     617    bool seed() {
     618#ifndef WIN32
     619      if (seedFromFile("/dev/urandom", 0)) return true;
     620#endif
     621      if (seedFromTime()) return true;
     622      return false;
     623    }
     624   
     625    /// \brief Seeding from file
     626    ///
     627    /// Seeding the random sequence from file. The linux kernel has two
     628    /// devices, <tt>/dev/random</tt> and <tt>/dev/urandom</tt> which
     629    /// could give good seed values for pseudo random generators (The
     630    /// difference between two devices is that the <tt>random</tt> may
     631    /// block the reading operation while the kernel can give good
     632    /// source of randomness, while the <tt>urandom</tt> does not
     633    /// block the input, but it could give back bytes with worse
     634    /// entropy).
     635    /// \param file The source file
     636    /// \param offset The offset, from the file read.
     637    /// \return True when the seeding is success.
     638#ifndef WIN32
     639    bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0)
     640#else
     641    bool seedFromFile(const std::string& file = "", int offset = 0)
     642#endif
     643    {
     644      std::ifstream rs(file.c_str());
     645      const int size = 4;
     646      Word buf[size];
     647      if (offset != 0 && !rs.seekg(offset)) return false;
     648      if (!rs.read(reinterpret_cast<char*>(buf), sizeof(buf))) return false;
     649      seed(buf, buf + size);
     650      return true;
     651    }
     652
     653    /// \brief Seding from process id and time
     654    ///
     655    /// Seding from process id and time. This function uses the
     656    /// current process id and the current time for initialize the
     657    /// random sequence.
     658    /// \return Currently always true.
     659    bool seedFromTime() {       
     660#ifndef WIN32
     661      timeval tv;
     662      gettimeofday(&tv, 0);
     663      seed(getpid() + tv.tv_sec + tv.tv_usec);
     664#else
     665      FILETIME time;
     666      GetSystemTimeAsFileTime(&time);
     667      seed(GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime);
     668#endif
     669      return true;
     670    }
     671
     672    /// @}
     673
     674    ///\name Uniform distributions
     675    ///
     676    /// @{
    596677
    597678    /// \brief Returns a random real number from the range [0, 1)
    598679    ///
     
    708789    bool boolean() {
    709790      return bool_producer.convert(core);
    710791    }
     792
     793    /// @}
    711794
    712795    ///\name Non-uniform distributions
    713796    ///