Changeset 738 for trunk/yat/random/random.cc
- Timestamp:
- Jan 8, 2007, 12:08:39 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/random/random.cc
r718 r738 24 24 #include "random.h" 25 25 #include "yat/statistics/Histogram.h" 26 #include "yat/utility/Exception.h" 26 27 27 28 #include <fstream> 28 #include < iostream>29 #include <sstream> 29 30 30 31 namespace theplu { … … 36 37 RNG::RNG(void) 37 38 { 38 gsl_rng_env_setup(); // support rng/seed changes through environment vars 39 rng_ = gsl_rng_alloc(gsl_rng_default); // Memory is allocated here! 39 // support rng/seed changes through environment vars 40 if (!gsl_rng_env_setup()) 41 throw utility::GSL_error("RNG: unknown generator"); 42 if (!(rng_=gsl_rng_alloc(gsl_rng_default))) 43 throw utility::GSL_error("RNG: failed to allocate memory"); 40 44 } 41 45 … … 105 109 RNG_state::RNG_state(const RNG* rng) 106 110 { 107 rng_ = gsl_rng_clone(rng->rng()); 111 if (!(rng_ = gsl_rng_clone(rng->rng()))) 112 throw utility::GSL_error("RNG_state: failed to allocate memory"); 108 113 } 109 114 … … 146 151 gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p ); 147 152 delete p; 153 if (!gen_) 154 throw utility::GSL_error("DiscreteGeneral: failed to setup generator."); 148 155 } 149 156 … … 163 170 164 171 165 DiscreteUniform::DiscreteUniform(void) 166 : range_(rng_->max()) 167 { 168 } 169 170 DiscreteUniform::DiscreteUniform(const u_long n) 172 DiscreteUniform::DiscreteUniform(u_long n) 171 173 : range_(n) 172 174 { 173 if (range_>rng_->max()) 174 range_=rng_->max(); 175 if (range_>rng_->max()) { 176 std::stringstream ss("DiscreteUniform: "); 177 ss << n << " is too large for RNG " << rng_->name(); 178 throw utility::GSL_error(ss.str()); 179 } 175 180 } 176 181 … … 178 183 u_long DiscreteUniform::operator()(void) const 179 184 { 180 return gsl_rng_uniform_int(rng_->rng(), range_); 181 } 182 183 184 u_long DiscreteUniform::operator()(const u_long n) const 185 { 186 return gsl_rng_uniform_int(rng_->rng(), n); 185 return (range_ ? 186 gsl_rng_uniform_int(rng_->rng(), range_) : gsl_rng_get(rng_->rng())); 187 } 188 189 190 u_long DiscreteUniform::operator()(u_long n) const 191 { 192 // making sure that n is not larger than the range of the 193 // underlying RNG 194 if (n>rng_->max()) { 195 std::stringstream ss("DiscreteUniform::op(): "); 196 ss << n << " is too large for RNG " << rng_->name(); 197 throw utility::GSL_error(ss.str()); 198 } 199 return gsl_rng_uniform_int(rng_->rng(),n); 187 200 } 188 201
Note: See TracChangeset
for help on using the changeset viewer.