Merge
authorAlpar Juttner <alpar@cs.elte.hu>
Fri, 20 Jun 2008 11:09:30 +0100
changeset 1830c6556a8e105
parent 182 9c6dfb5141d3
parent 178 d2bac07f1742
child 184 716b220697a0
Merge
     1.1 --- a/configure.ac	Wed Jun 18 13:52:23 2008 +0100
     1.2 +++ b/configure.ac	Fri Jun 20 11:09:30 2008 +0100
     1.3 @@ -1,13 +1,9 @@
     1.4  dnl Process this file with autoconf to produce a configure script.
     1.5  
     1.6  dnl Version information.
     1.7 -m4_define([lemon_version_major], [0])
     1.8 -m4_define([lemon_version_minor], [99])
     1.9 -m4_define([lemon_version_micro], [])
    1.10 -m4_define([lemon_version_nano], [])
    1.11 -m4_define([lemon_version_tag], [hg])
    1.12 +m4_define([lemon_version_number], [])
    1.13  m4_define([lemon_hg_revision], [m4_normalize(esyscmd([hg id -i]))])
    1.14 -m4_define([lemon_version], [lemon_version_major().lemon_version_minor()ifelse(lemon_version_micro(), [], [], [.lemon_version_micro()])ifelse(lemon_version_nano(), [], [], [.lemon_version_nano()])ifelse(lemon_version_tag(), [], [], lemon_version_tag(), [hg], [[_]lemon_version_tag()[_]lemon_hg_revision()], [[_]lemon_version_tag()])])
    1.15 +m4_define([lemon_version], [ifelse(lemon_version_number(), [], [lemon_hg_revision()], [lemon_version_number()])])
    1.16  
    1.17  AC_PREREQ([2.59])
    1.18  AC_INIT([LEMON], [lemon_version()], [lemon-devel@lemon.cs.elte.hu], [lemon])
     2.1 --- a/lemon/random.h	Wed Jun 18 13:52:23 2008 +0100
     2.2 +++ b/lemon/random.h	Fri Jun 20 11:09:30 2008 +0100
     2.3 @@ -66,10 +66,20 @@
     2.4  #include <iterator>
     2.5  #include <vector>
     2.6  #include <limits>
     2.7 +#include <fstream>
     2.8  
     2.9  #include <lemon/math.h>
    2.10  #include <lemon/dim2.h>
    2.11  
    2.12 +#ifndef WIN32
    2.13 +#include <sys/time.h>
    2.14 +#include <ctime>
    2.15 +#include <sys/types.h>
    2.16 +#include <unistd.h>
    2.17 +#else
    2.18 +#include <windows.h>
    2.19 +#endif
    2.20 +
    2.21  ///\ingroup misc
    2.22  ///\file
    2.23  ///\brief Mersenne Twister random number generator
    2.24 @@ -526,6 +536,14 @@
    2.25  
    2.26    public:
    2.27  
    2.28 +    ///\name Initialization
    2.29 +    ///
    2.30 +    /// @{
    2.31 +
    2.32 +    ///\name Initialization
    2.33 +    ///
    2.34 +    /// @{
    2.35 +
    2.36      /// \brief Default constructor
    2.37      ///
    2.38      /// Constructor with constant seeding.
    2.39 @@ -594,6 +612,73 @@
    2.40        _random_bits::Initializer<Number, Word>::init(core, begin, end);
    2.41      }
    2.42  
    2.43 +    /// \brief Seeding from file or from process id and time
    2.44 +    ///
    2.45 +    /// By default, this function calls the \c seedFromFile() member
    2.46 +    /// function with the <tt>/dev/urandom</tt> file. If it does not success,
    2.47 +    /// it uses the \c seedFromTime().
    2.48 +    /// \return Currently always true.
    2.49 +    bool seed() {
    2.50 +#ifndef WIN32
    2.51 +      if (seedFromFile("/dev/urandom", 0)) return true;
    2.52 +#endif
    2.53 +      if (seedFromTime()) return true;
    2.54 +      return false;
    2.55 +    }
    2.56 +    
    2.57 +    /// \brief Seeding from file
    2.58 +    ///
    2.59 +    /// Seeding the random sequence from file. The linux kernel has two
    2.60 +    /// devices, <tt>/dev/random</tt> and <tt>/dev/urandom</tt> which
    2.61 +    /// could give good seed values for pseudo random generators (The
    2.62 +    /// difference between two devices is that the <tt>random</tt> may
    2.63 +    /// block the reading operation while the kernel can give good
    2.64 +    /// source of randomness, while the <tt>urandom</tt> does not
    2.65 +    /// block the input, but it could give back bytes with worse
    2.66 +    /// entropy).
    2.67 +    /// \param file The source file
    2.68 +    /// \param offset The offset, from the file read.
    2.69 +    /// \return True when the seeding successes.
    2.70 +#ifndef WIN32
    2.71 +    bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0) 
    2.72 +#else
    2.73 +    bool seedFromFile(const std::string& file = "", int offset = 0) 
    2.74 +#endif
    2.75 +    {
    2.76 +      std::ifstream rs(file.c_str());
    2.77 +      const int size = 4;
    2.78 +      Word buf[size];
    2.79 +      if (offset != 0 && !rs.seekg(offset)) return false;
    2.80 +      if (!rs.read(reinterpret_cast<char*>(buf), sizeof(buf))) return false;
    2.81 +      seed(buf, buf + size);
    2.82 +      return true;
    2.83 +    }
    2.84 +
    2.85 +    /// \brief Seding from process id and time
    2.86 +    ///
    2.87 +    /// Seding from process id and time. This function uses the
    2.88 +    /// current process id and the current time for initialize the
    2.89 +    /// random sequence.
    2.90 +    /// \return Currently always true.
    2.91 +    bool seedFromTime() { 	
    2.92 +#ifndef WIN32
    2.93 +      timeval tv;
    2.94 +      gettimeofday(&tv, 0);
    2.95 +      seed(getpid() + tv.tv_sec + tv.tv_usec);
    2.96 +#else
    2.97 +      FILETIME time;
    2.98 +      GetSystemTimeAsFileTime(&time);
    2.99 +      seed(GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime);
   2.100 +#endif
   2.101 +      return true;
   2.102 +    }
   2.103 +
   2.104 +    /// @}
   2.105 +
   2.106 +    ///\name Uniform distributions
   2.107 +    ///
   2.108 +    /// @{
   2.109 +
   2.110      /// \brief Returns a random real number from the range [0, 1)
   2.111      ///
   2.112      /// It returns a random real number from the range [0, 1). The
   2.113 @@ -623,6 +708,12 @@
   2.114        return real<Number>() * (b - a) + a; 
   2.115      }
   2.116  
   2.117 +    /// @}
   2.118 +
   2.119 +    ///\name Uniform distributions
   2.120 +    ///
   2.121 +    /// @{
   2.122 +
   2.123      /// \brief Returns a random real number from the range [0, 1)
   2.124      ///
   2.125      /// It returns a random double from the range [0, 1).
   2.126 @@ -680,6 +771,8 @@
   2.127        return _random_bits::IntConversion<Number, Word>::convert(core);
   2.128      }
   2.129  
   2.130 +    /// @}
   2.131 +
   2.132      unsigned int uinteger() {
   2.133        return uinteger<unsigned int>();
   2.134      }
   2.135 @@ -709,6 +802,8 @@
   2.136        return bool_producer.convert(core);
   2.137      }
   2.138  
   2.139 +    /// @}
   2.140 +
   2.141      ///\name Non-uniform distributions
   2.142      ///
   2.143