hashRandom.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hashRandom.cc
Go to the documentation of this file.
1 /* hashRandom.cc
2 */
3 #include "osl/hash/hashRandom.h"
4 #include "osl/misc/milliSeconds.h"
5 // #include <boost/random/normal_distribution.hpp>
6 #include <boost/random/mersenne_twister.hpp>
7 #include <boost/random/uniform_01.hpp>
8 #include <boost/noncopyable.hpp>
9 #include <cmath>
10
11 osl::CArray<int,osl::hash::HashRandom::Length> osl::hash::HashRandom::table;
12
13 // ---------------------------------------------------------------
14 // 2009-06-12 A slightly modified version of boost::normal_distribution is defined here.
15 // It is because g++ 4.3 and 4.4 warn about uninitialized values of _r1 and _cached_rho in the original source code.
16 // This should be temporal and will be removed in near future.
17 // ---------------------------------------------------------------
18 /* boost random/normal_distribution.hpp header file
19 *
20 * Copyright Jens Maurer 2000-2001
21 * Distributed under the Boost Software License, Version 1.0. (See
22 * accompanying file LICENSE_1_0.txt or copy at
23 * http://www.boost.org/LICENSE_1_0.txt)
24 *
25 * See http://www.boost.org for most recent version including documentation.
26 *
27 * $Id: normal_distribution.hpp 49314 2008-10-13 09:00:03Z johnmaddock $
28 *
29 * Revision history
30 * 2001-02-18 moved to individual header files
31 */
32 namespace osl
33 {
34 class normal_distribution : boost::noncopyable
35 {
36 public:
37 explicit normal_distribution(const double& mean_arg = double(0),
38 const double& sigma_arg = double(1))
39 : _mean(mean_arg), _sigma(sigma_arg),
40 _r1(0), _r2(0), _cached_rho(0),
41 _valid(false)
42 {
43 assert(_sigma >= double(0));
44 }
45 double mean() const { return _mean; }
46 double sigma() const { return _sigma; }
47 void reset() { _valid = false; }
48 template<class Engine>
49 double operator()(Engine& eng)
50 {
51 if(!_valid) {
52 _r1 = eng();
53 _r2 = eng();
54 _cached_rho = sqrt(-double(2) * log(double(1)-_r2));
55 _valid = true;
56 } else {
57 _valid = false;
58 }
59 const double pi = double(3.14159265358979323846);
60 return _cached_rho * (_valid ?
61 cos(double(2)*pi*_r1) :
62 sin(double(2)*pi*_r1))
63 * _sigma + _mean;
64 }
65 private:
66 double _mean, _sigma;
67 double _r1, _r2, _cached_rho;
68 bool _valid;
69 };
70 }
71 // ---------------------------------------------------------------
72 // end of modified version of boost::normal_distribution
73 // ---------------------------------------------------------------
74
75
76 void osl::hash::HashRandom::setUp(double sigma)
77 {
78 static boost::mt11213b mt_random(MilliSeconds::now().value());
79 static boost::uniform_01<boost::mt11213b> u0(mt_random);
80 // boost::normal_distribution<> n(0, sigma);
81 normal_distribution n(0, sigma);
82 for (size_t i=0; i<Length; ++i)
83 table[i] = static_cast<int>(n(u0))/2*2;
84 }
85