1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
5 This file is part of libDAI.
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <boost/random/linear_congruential.hpp>
25 #include <boost/random/uniform_real.hpp>
26 #include <boost/random/normal_distribution.hpp>
27 #include <boost/random/variate_generator.hpp>
31 #include <boost/math/special_functions/atanh.hpp> // for atanh
32 #include <boost/math/special_functions/log1p.hpp> // for log1p
33 #include <float.h> // for _isnan
35 // Assume POSIX compliant system. We need the following for querying the CPU time for this process
36 #include <sys/times.h>
37 #include <sys/param.h>
42 bool isnan( double x
) {
45 double atanh( double x
) {
46 return boost::math::atanh( x
);
48 double log1p( double x
) {
49 return boost::math::log1p( x
);
57 // Returns user+system time in seconds
62 return( (double)(tbuf
.wSecond
+ (double)tbuf
.wMilliseconds
/ 1000.0) );
66 return( (double)(tbuf
.tms_utime
+ tbuf
.tms_stime
) / HZ
);
70 // This is a typedef for a random number generator.
71 // Try boost::mt19937 or boost::ecuyer1988 instead of boost::minstd_rand
72 typedef boost::minstd_rand _rnd_gen_type
;
74 _rnd_gen_type
_rnd_gen(42U);
76 // Define a uniform random number distribution which produces
77 // values between 0 and 1 (0 inclusive, 1 exclusive).
78 boost::uniform_real
<> _uni_dist(0,1);
79 boost::variate_generator
<_rnd_gen_type
&, boost::uniform_real
<> > _uni_rnd(_rnd_gen
, _uni_dist
);
81 // Define a normal distribution with mean 0 and standard deviation 1.
82 boost::normal_distribution
<> _normal_dist
;
83 boost::variate_generator
<_rnd_gen_type
&, boost::normal_distribution
<> > _normal_rnd(_rnd_gen
, _normal_dist
);
86 void rnd_seed( size_t seed
) {
90 double rnd_uniform() {
94 double rnd_stdnormal() {
98 int rnd_int( int min
, int max
) {
99 return (int)floor(_uni_rnd() * (max
+ 1 - min
) + min
);
103 } // end of namespace dai