lemon/random.h
changeset 783 ef88c0a30f85
parent 559 c5fd2d996909
child 1139 d51126dc39fa
     1.1 --- a/lemon/random.h	Mon Jan 12 23:11:39 2009 +0100
     1.2 +++ b/lemon/random.h	Thu Nov 05 15:48:01 2009 +0100
     1.3 @@ -77,7 +77,7 @@
     1.4  #include <sys/types.h>
     1.5  #include <unistd.h>
     1.6  #else
     1.7 -#include <windows.h>
     1.8 +#include <lemon/bits/windows.h>
     1.9  #endif
    1.10  
    1.11  ///\ingroup misc
    1.12 @@ -344,56 +344,46 @@
    1.13        }
    1.14      };
    1.15  
    1.16 -    template <typename Result, int exp, bool pos = (exp >= 0)>
    1.17 +    template <typename Result, int exp>
    1.18      struct ShiftMultiplier {
    1.19        static const Result multiplier() {
    1.20          Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
    1.21          res *= res;
    1.22 -        if ((exp & 1) == 1) res *= static_cast<Result>(2.0);
    1.23 -        return res;
    1.24 -      }
    1.25 -    };
    1.26 -
    1.27 -    template <typename Result, int exp>
    1.28 -    struct ShiftMultiplier<Result, exp, false> {
    1.29 -      static const Result multiplier() {
    1.30 -        Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
    1.31 -        res *= res;
    1.32          if ((exp & 1) == 1) res *= static_cast<Result>(0.5);
    1.33          return res;
    1.34        }
    1.35      };
    1.36  
    1.37      template <typename Result>
    1.38 -    struct ShiftMultiplier<Result, 0, true> {
    1.39 +    struct ShiftMultiplier<Result, 0> {
    1.40        static const Result multiplier() {
    1.41          return static_cast<Result>(1.0);
    1.42        }
    1.43      };
    1.44  
    1.45      template <typename Result>
    1.46 -    struct ShiftMultiplier<Result, -20, true> {
    1.47 +    struct ShiftMultiplier<Result, 20> {
    1.48        static const Result multiplier() {
    1.49          return static_cast<Result>(1.0/1048576.0);
    1.50        }
    1.51      };
    1.52  
    1.53      template <typename Result>
    1.54 -    struct ShiftMultiplier<Result, -32, true> {
    1.55 +    struct ShiftMultiplier<Result, 32> {
    1.56        static const Result multiplier() {
    1.57 -        return static_cast<Result>(1.0/424967296.0);
    1.58 +        return static_cast<Result>(1.0/4294967296.0);
    1.59        }
    1.60      };
    1.61  
    1.62      template <typename Result>
    1.63 -    struct ShiftMultiplier<Result, -53, true> {
    1.64 +    struct ShiftMultiplier<Result, 53> {
    1.65        static const Result multiplier() {
    1.66          return static_cast<Result>(1.0/9007199254740992.0);
    1.67        }
    1.68      };
    1.69  
    1.70      template <typename Result>
    1.71 -    struct ShiftMultiplier<Result, -64, true> {
    1.72 +    struct ShiftMultiplier<Result, 64> {
    1.73        static const Result multiplier() {
    1.74          return static_cast<Result>(1.0/18446744073709551616.0);
    1.75        }
    1.76 @@ -413,7 +403,7 @@
    1.77        static const int bits = std::numeric_limits<Word>::digits;
    1.78  
    1.79        static Result convert(RandomCore<Word>& rnd) {
    1.80 -        return Shifting<Result, - shift - rest>::
    1.81 +        return Shifting<Result, shift + rest>::
    1.82            shift(static_cast<Result>(rnd() >> (bits - rest)));
    1.83        }
    1.84      };
    1.85 @@ -423,7 +413,7 @@
    1.86        static const int bits = std::numeric_limits<Word>::digits;
    1.87  
    1.88        static Result convert(RandomCore<Word>& rnd) {
    1.89 -        return Shifting<Result, - shift - bits>::
    1.90 +        return Shifting<Result, shift + bits>::
    1.91            shift(static_cast<Result>(rnd())) +
    1.92            RealConversion<Result, Word, rest-bits, shift + bits>::
    1.93            convert(rnd);
    1.94 @@ -613,7 +603,7 @@
    1.95      /// By default, this function calls the \c seedFromFile() member
    1.96      /// function with the <tt>/dev/urandom</tt> file. If it does not success,
    1.97      /// it uses the \c seedFromTime().
    1.98 -    /// \return Currently always true.
    1.99 +    /// \return Currently always \c true.
   1.100      bool seed() {
   1.101  #ifndef WIN32
   1.102        if (seedFromFile("/dev/urandom", 0)) return true;
   1.103 @@ -634,7 +624,7 @@
   1.104      /// entropy).
   1.105      /// \param file The source file
   1.106      /// \param offset The offset, from the file read.
   1.107 -    /// \return True when the seeding successes.
   1.108 +    /// \return \c true when the seeding successes.
   1.109  #ifndef WIN32
   1.110      bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0)
   1.111  #else
   1.112 @@ -655,23 +645,21 @@
   1.113      /// Seding from process id and time. This function uses the
   1.114      /// current process id and the current time for initialize the
   1.115      /// random sequence.
   1.116 -    /// \return Currently always true.
   1.117 +    /// \return Currently always \c true.
   1.118      bool seedFromTime() {
   1.119  #ifndef WIN32
   1.120        timeval tv;
   1.121        gettimeofday(&tv, 0);
   1.122        seed(getpid() + tv.tv_sec + tv.tv_usec);
   1.123  #else
   1.124 -      FILETIME time;
   1.125 -      GetSystemTimeAsFileTime(&time);
   1.126 -      seed(GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime);
   1.127 +      seed(bits::getWinRndSeed());
   1.128  #endif
   1.129        return true;
   1.130      }
   1.131  
   1.132      /// @}
   1.133  
   1.134 -    ///\name Uniform distributions
   1.135 +    ///\name Uniform Distributions
   1.136      ///
   1.137      /// @{
   1.138  
   1.139 @@ -774,7 +762,7 @@
   1.140  
   1.141      /// @}
   1.142  
   1.143 -    ///\name Non-uniform distributions
   1.144 +    ///\name Non-uniform Distributions
   1.145      ///
   1.146      ///@{
   1.147  
   1.148 @@ -950,7 +938,7 @@
   1.149  
   1.150      ///@}
   1.151  
   1.152 -    ///\name Two dimensional distributions
   1.153 +    ///\name Two Dimensional Distributions
   1.154      ///
   1.155      ///@{
   1.156