Changeset 362


Ignore:
Timestamp:
Aug 5, 2005, 12:22:23 AM (18 years ago)
Author:
Jari Häkkinen
Message:

First usable minimal version of new Random.

Location:
trunk/lib/utility
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/utility/Makefile.am

    r341 r362  
    1010libutility_la_SOURCES = \
    1111  Alignment.cc FileIO.cc kNNI.cc Merge.cc NNI.cc PCA.cc\
    12   random_singleton.cc stl_utility.cc SVD.cc utility.cc WeNNI.cc
     12  Random.cc random_singleton.cc stl_utility.cc SVD.cc utility.cc WeNNI.cc
    1313
    1414include_utilitydir = $(includedir)/c++_tools/utility
     
    1616include_utility_HEADERS = \
    1717  Alignment.h Exception.h FileIO.h kNNI.h Merge.h NNI.h PCA.h \
    18   random_singleton.h stl_utility.h SVD.h utility.h WeNNI.h
     18  Random.h random_singleton.h stl_utility.h SVD.h utility.h WeNNI.h
    1919
  • trunk/lib/utility/Random.h

    r361 r362  
    1 // $Id$
     1 // $Id$
    22
    33#ifndef _theplu_utility_random_
    44#define _theplu_utility_random_
    55
    6 // #include <string>
    76
    8 // #include <gsl/gsl_rng.h>
    9 // #include <gsl/gsl_randist.h>
     7#include <gsl/gsl_rng.h>
     8#include <gsl/gsl_randist.h>
    109
    1110namespace theplu {
     
    1312
    1413  ///
    15   /// Base class
     14  /// The RNG class provides a single global random number generator
     15  /// instance with one point of access to the generator.
    1616  ///
    17   class Random {
     17  /// This is probably not thread safe.
     18  ///
     19  /// @see Design Patterns (the singleton and adapter pattern)
     20  ///
     21  class RNG
     22  {
    1823  public:
    1924
     25    virtual ~RNG(void);
     26
     27    static RNG* instance(int seed);
     28
     29    inline const gsl_rng* rng(void) const { return rng_; }
     30
    2031  private:
    21     Random(void);
    2232
    23     gsl_rng* rng_;
     33    RNG(void);
     34
     35    static RNG* instance_;
     36    gsl_rng* rng_;
    2437  };
    2538
    2639
    27   class RandomContinuous {
     40
     41  class RandomContinuous
     42  {
     43  public:
     44
     45    inline RandomContinuous(void) { rng_=RNG::instance(89); }
     46
     47    virtual double operator()(void) const = 0;
     48
     49  protected:
     50    RNG* rng_;
    2851  };
    2952
    3053
    31   class RandomContinuousUniform : public RandomContinuous {
     54
     55  class RandomContinuousUniform : public RandomContinuous
     56  {
     57  public:
     58
     59    inline double operator()(void) const { return gsl_rng_uniform(rng_->rng()); }
     60
     61  };
     62
     63
     64
     65  class RandomGaussian : public RandomContinuous
     66  {
     67  public:
     68    inline RandomGaussian(const double s=1, const double m=0)
     69      : m_(m), s_(s) {}
     70
     71    inline double operator()(void) const
     72    { return gsl_ran_gaussian(rng_->rng(), s_)+m_; }
     73
     74    inline double operator()(const double s) const
     75    { return gsl_ran_gaussian(rng_->rng(), s); }
     76
     77    inline double operator()(const double s, const double m) const
     78    { return gsl_ran_gaussian(rng_->rng(), s)+m; }
     79
     80  private:
     81    double m_;
     82    double s_;
    3283  };
    3384
Note: See TracChangeset for help on using the changeset viewer.