1 | // $Id: random.cc 706 2006-12-19 08:59:19Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 2 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "random.h" |
---|
25 | #include "yat/statistics/Histogram.h" |
---|
26 | |
---|
27 | #include <fstream> |
---|
28 | #include <iostream> |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace random { |
---|
33 | |
---|
34 | RNG* RNG::instance_ = NULL; |
---|
35 | |
---|
36 | |
---|
37 | RNG::RNG(void) |
---|
38 | { |
---|
39 | gsl_rng_env_setup(); // support rng/seed changes through environment vars |
---|
40 | rng_ = gsl_rng_alloc(gsl_rng_default); // Memory is allocated here! |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | RNG::~RNG(void) |
---|
45 | { |
---|
46 | gsl_rng_free(rng_); |
---|
47 | rng_=NULL; |
---|
48 | delete instance_; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | u_long RNG::seed_from_devurandom(void) |
---|
53 | { |
---|
54 | u_char ulongsize=sizeof(u_long); |
---|
55 | char* buffer=new char[ulongsize]; |
---|
56 | std::ifstream is("/dev/urandom"); |
---|
57 | is.read(buffer,ulongsize); |
---|
58 | is.close(); |
---|
59 | u_long s=0; |
---|
60 | for (u_int i=0; i<ulongsize; i++) { |
---|
61 | u_char ch=buffer[i]; |
---|
62 | s+=ch<<((ulongsize-1-i)*8); |
---|
63 | } |
---|
64 | seed(s); |
---|
65 | return s; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | int RNG::set_state(const RNG_state& state) |
---|
70 | { |
---|
71 | return gsl_rng_memcpy(rng_, state.rng()); |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | RNG_state::RNG_state(const RNG* rng) |
---|
76 | { |
---|
77 | rng_ = gsl_rng_clone(rng->rng()); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | RNG_state::~RNG_state(void) |
---|
82 | { |
---|
83 | gsl_rng_free(rng_); |
---|
84 | rng_=NULL; |
---|
85 | } |
---|
86 | |
---|
87 | // --------------------- Discrete distribtuions --------------------- |
---|
88 | |
---|
89 | Discrete::Discrete(void) |
---|
90 | : rng_(RNG::instance()) |
---|
91 | { |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | Discrete::~Discrete(void) |
---|
96 | { |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist) |
---|
101 | { |
---|
102 | double* p = new double[ hist.nof_bins() ]; |
---|
103 | for (size_t i=0; i<hist.nof_bins(); i++) |
---|
104 | p[ i ] = hist[i]; |
---|
105 | gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p ); |
---|
106 | delete p; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | DiscreteGeneral::~DiscreteGeneral(void) |
---|
111 | { |
---|
112 | if (gen_) |
---|
113 | gsl_ran_discrete_free( gen_ ); |
---|
114 | gen_ = NULL; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | Poisson::Poisson(const double m) |
---|
119 | : m_(m) |
---|
120 | { |
---|
121 | } |
---|
122 | |
---|
123 | // --------------------- Continuous distribtuions --------------------- |
---|
124 | |
---|
125 | Continuous::Continuous(void) |
---|
126 | : rng_(RNG::instance()) |
---|
127 | { |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | Continuous::~Continuous(void) |
---|
132 | { |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | ContinuousGeneral::ContinuousGeneral(const statistics::Histogram& hist) |
---|
137 | : discrete_(DiscreteGeneral(hist)), hist_(hist) |
---|
138 | { |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | Exponential::Exponential(const double m) |
---|
143 | : m_(m) |
---|
144 | { |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | Gaussian::Gaussian(const double s, const double m) |
---|
149 | : m_(m), s_(s) |
---|
150 | { |
---|
151 | } |
---|
152 | |
---|
153 | }}} // of namespace random, yat, and theplu |
---|