[Lemon-commits] Balazs Dezso: Seeding from file source or from p...

Lemon HG hg at lemon.cs.elte.hu
Thu Jun 19 18:30:49 CEST 2008


details:   http://lemon.cs.elte.hu/hg/lemon/rev/b685e12e08c0
changeset: 177:b685e12e08c0
user:      Balazs Dezso <deba [at] inf.elte.hu>
date:      Thu Jun 19 10:59:22 2008 +0200
description:
	Seeding from file source or from pid and time (ticket #19)

diffstat:

1 file changed, 83 insertions(+)
lemon/random.h |   83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++

diffs (117 lines):

diff -r 47b69d4b0759 -r b685e12e08c0 lemon/random.h
--- a/lemon/random.h	Tue Jun 17 12:47:15 2008 +0100
+++ b/lemon/random.h	Thu Jun 19 10:59:22 2008 +0200
@@ -66,9 +66,19 @@
 #include <iterator>
 #include <vector>
 #include <limits>
+#include <fstream>
 
 #include <lemon/math.h>
 #include <lemon/dim2.h>
+
+#ifndef WIN32
+#include <sys/time.h>
+#include <ctime>
+#include <sys/types.h>
+#include <unistd.h>
+#else
+#include <windows.h>
+#endif
 
 ///\ingroup misc
 ///\file
@@ -526,6 +536,10 @@
 
   public:
 
+    ///\name Initialization
+    ///
+    /// @{
+
     /// \brief Default constructor
     ///
     /// Constructor with constant seeding.
@@ -593,6 +607,73 @@
       typedef typename std::iterator_traits<Iterator>::value_type Number;
       _random_bits::Initializer<Number, Word>::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 <tt>/dev/urandom</tt> 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, <tt>/dev/random</tt> and <tt>/dev/urandom</tt> which
+    /// could give good seed values for pseudo random generators (The
+    /// difference between two devices is that the <tt>random</tt> may
+    /// block the reading operation while the kernel can give good
+    /// source of randomness, while the <tt>urandom</tt> 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<char*>(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)
     ///
@@ -708,6 +789,8 @@
     bool boolean() {
       return bool_producer.convert(core);
     }
+
+    /// @}
 
     ///\name Non-uniform distributions
     ///



More information about the Lemon-commits mailing list