Changeset 379


Ignore:
Timestamp:
Aug 8, 2005, 4:39:24 PM (17 years ago)
Author:
Peter
Message:

removed min parameter in uniform_int and added test to rnd_test

Location:
trunk
Files:
3 edited

Legend:

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

    r375 r379  
    6060
    6161
     62  DiscreteUniform::DiscreteUniform(void)
     63    : range_(rng_->max()+1)
     64  {
     65  }
     66
     67
     68
     69  DiscreteUniform::DiscreteUniform(const u_long range)
     70    : range_(range)
     71  {
     72
     73    if ( range_-1 > rng_->max()){
     74      range_ = rng_->max()+1;
     75    }
     76       
     77
     78
     79  }
     80
     81
     82
    6283  DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist)
    6384  {
     
    79100
    80101
     102
    81103}} // of namespace random and namespace theplu
  • trunk/lib/random/random.h

    r378 r379  
    2727  /// There are many different rng's available in GSL. Currently only
    2828  /// the default generator is implemented and no other one is
    29   /// choosable though the class interface. This means that you have
     29  /// choosable through the class interface. This means that you have
    3030  /// to fall back to the use of environment variables as described in
    3131  /// the GSL documentation, or be bold and request support for other
     
    251251    /// @return A random number.
    252252    ///
    253     virtual long operator()(void) const = 0;
     253    virtual u_long operator()(void) const = 0;
    254254   
    255255  protected:
     
    262262  /// Discrete uniform distribution also known as the "equally likely
    263263  /// outcomes" distribution. Each outcome, in this case an integer
    264   /// from \f$ [a,b) \f$, have equal probability to occur.
     264  /// from [a,b) , have equal probability to occur.
    265265  ///
    266266  /// Distribution function \f$ p(k) = \frac{1}{b-a} \f$ for \f$ a \le
    267267  /// k < b \f$ \n
    268   /// Expectation value: \f$ \frac{a+b}{2} \f$ \n
     268  /// Expectation value: \f$ \frac{a+b-1}{2} \f$ \n
    269269  /// Variance: \f$ \frac{1}{3}((b-a)^2-1) \f$
    270270  ///
     
    276276    /// @brief Constructor.
    277277    ///
    278     /// @todo The default behaviour of the underlying RNG should be
    279     /// setup when this constructor is used.
    280     ///
    281     inline DiscreteUniform(void)
    282       : min_(0), range_(0) {}
     278    DiscreteUniform(void);
    283279
    284280    ///
    285281    /// @brief Constructor.
    286282    ///
    287     /// @param \a range is number of different integers that can be
    288     /// generated \f$ (b-a+1) \f$ \a min is the minimal integer that
    289     /// can be generated \f$ (a) \f$
    290     ///
    291     inline DiscreteUniform(const u_long range, const long min=0)
    292       : min_(min), range_(range) {}
    293 
    294     ///
    295     /// @return A random number between \a min_ and \a range_ - \a min_
    296     ///
    297     inline long operator()(void) const
    298     { return gsl_rng_uniform_int(rng_->rng(), range_)+min_; }
    299 
    300     ///
    301     /// @return A random number between 0 and \a range.
    302     ///
    303     /// @note This operator ignores any set minimum range parameter.
    304     ///
    305     inline long operator()(const u_long range) const
    306     { return gsl_rng_uniform_int(rng_->rng(), range); }
    307 
    308     ///
    309     /// @return A random number between \a min and \a range - \a min
    310     ///
    311     inline long operator()(const u_long range, const long min) const
    312     { return gsl_rng_uniform_int(rng_->rng(), range)+min; }
    313 
    314 
    315   private:
    316     long min_;
     283    /// @param n sets the range. Object will generate integers from
     284    /// [0,n-1].
     285    ///
     286    /// @todo exception should be thrown, when n is out of range
     287    ///
     288    DiscreteUniform(const u_long n);
     289     
     290    ///
     291    /// This function returns a random integer from 0 to n-1
     292    /// inclusive. All integers in the range [0,n-1] are equally
     293    /// likely. n is set in constructor.
     294    ///
     295    inline u_long operator()(void) const
     296    { return gsl_rng_uniform_int(rng_->rng(), range_); }
     297
     298    ///
     299    /// This function returns a random integer from 0 to n-1
     300    /// inclusive. All integers in the range [0,n-1] are equally
     301    /// likely.
     302    ///
     303    /// @todo exception should be thrown, when n is out of range
     304    ///
     305    inline u_long operator()(const u_long n) const
     306    { return gsl_rng_uniform_int(rng_->rng(), n); }
     307
     308
     309  private:
    317310    u_long range_;
    318311  };
     
    347340    /// @return A Poisson distributed number.
    348341    ///
    349     inline long operator()(void) const
     342    inline u_long operator()(void) const
    350343    { return gsl_ran_poisson(rng_->rng(), m_); }
    351344
     
    387380    /// @return A random number.
    388381    ///
    389     inline long operator()(void) const
     382    inline u_long operator()(void) const
    390383    { return gsl_ran_discrete(rng_->rng(), gen_); }
    391384
  • trunk/test/rnd_test.cc

    r377 r379  
    44
    55#include <iostream>
    6 
     6#include <sstream>
     7#include <fstream>
    78
    89int main(const int argc,const char* argv[])
    910{
     11  std::ostream* error;
     12  if (argc>1 && argv[1]==std::string("-p"))
     13    error = &std::cerr;
     14  else
     15    error = new std::ofstream("/dev/null");
     16 
     17  bool ok = true;
     18
    1019  theplu::random::ContinuousUniform uniform;
    1120
    1221  theplu::random::RNG* rng=theplu::random::RNG::instance();
    1322
     23  // testing that minimal integer is zero for our generator
     24  if (rng->min()){
     25    *error << "Error: rng->min is not zero" << std::endl;
     26    ok = false;
     27  }
     28
     29  if (error!=&std::cerr)
     30    delete error;
     31
     32  if (!ok)
     33    return -1;
    1434  return 0;
    1535}
Note: See TracChangeset for help on using the changeset viewer.