Ignore:
Timestamp:
Jan 8, 2007, 12:08:39 AM (17 years ago)
Author:
Jari Häkkinen
Message:

Fixes #8 and #179, addresses #2.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/random/random.cc

    r718 r738  
    2424#include "random.h"
    2525#include "yat/statistics/Histogram.h"
     26#include "yat/utility/Exception.h"
    2627
    2728#include <fstream>
    28 #include <iostream>
     29#include <sstream>
    2930
    3031namespace theplu {
     
    3637  RNG::RNG(void)
    3738  {
    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");
    4044  }
    4145
     
    105109  RNG_state::RNG_state(const RNG* rng)
    106110  {
    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");
    108113  }
    109114 
     
    146151    gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p );
    147152    delete p;
     153    if (!gen_)
     154      throw utility::GSL_error("DiscreteGeneral: failed to setup generator.");
    148155  }
    149156
     
    163170
    164171
    165   DiscreteUniform::DiscreteUniform(void)
    166     : range_(rng_->max())
    167   {
    168   }
    169 
    170   DiscreteUniform::DiscreteUniform(const u_long n)
     172  DiscreteUniform::DiscreteUniform(u_long n)
    171173    : range_(n)
    172174  {
    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    }
    175180  }
    176181
     
    178183  u_long DiscreteUniform::operator()(void) const
    179184  {
    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);
    187200  }
    188201
Note: See TracChangeset for help on using the changeset viewer.