Changeset 425


Ignore:
Timestamp:
Dec 7, 2005, 3:14:24 PM (17 years ago)
Author:
Jari Häkkinen
Message:

Fixed bug in DiscreteUniform?.
Added seeding support for classes derived from Discrete and Continuous
virtual classes.

Location:
trunk
Files:
3 edited

Legend:

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

    r424 r425  
    1515
    1616
    17   RNG::RNG(u_long s)
     17  RNG::RNG(void)
    1818  {
    1919    gsl_rng_env_setup();  // support rng/seed changes through environment vars
    2020    rng_ = gsl_rng_alloc(gsl_rng_default);  // Memory is allocated here!
    21     seed(s);
    2221  }
    2322
  • trunk/lib/random/random.h

    r424 r425  
    4747  /// documentation.
    4848  ///
    49   /// @todo Is this class properly implemented? The underlying random
    50   /// number genereator should be a singleton, while allowing for
    51   /// several different distribution to be used. Jari's feeling is
    52   /// that the current implementation restricts the use to only one
    53   /// distribution per binary!
     49  /// @todo Get those exceptions in! Should classes be considered
     50  /// const if underlying structures are changed such as GSL stuff?
    5451  ///
    5552  class RNG
     
    6057
    6158    ///
    62     /// @brief Get an instance of the random number
    63     /// generator/distribution.
     59    /// Get an instance of the random number generator. If the random
     60    /// number generator is not already created, the call will create
     61    /// a new generator and use the default seed. I needed, the seed
     62    /// must be changed with the seed or seed_from_devurandom member
     63    /// functions.
     64    ///
     65    /// @brief Get an instance of the random number generator.
    6466    ///
    6567    /// @return A pointer to the random number generator.
    6668    ///
    67     static RNG* instance(u_long seed=0)
    68       { if (!instance_) instance_=new RNG(seed); return instance_; }
     69    /// @see seed and seed_from_devurandom
     70    ///
     71    static RNG* instance(void)
     72      { if (!instance_) instance_=new RNG; return instance_; }
    6973
    7074    ///
     
    106110
    107111  private:
    108     RNG(u_long seed);
     112    RNG(void);
    109113
    110114    static RNG* instance_;
     
    122126  /// with its probability.
    123127  ///
    124   class Discrete 
     128  class Discrete
    125129  {
    126130  public:
     
    129133    ///
    130134    inline Discrete(void) { rng_=RNG::instance(); }
     135
     136    ///
     137    /// Set the seed to \a s in the underlying rng. If \a s is zero, a
     138    /// default value from the rng's original implementation is used
     139    /// (cf. GSL documentation).
     140    ///
     141    /// @brief Set the seed to \a s.
     142    ///
     143    /// @see seed_from_devurandom, RNG::seed_from_devurandom, RNG::seed
     144    ///
     145    inline void seed(u_long s) const { rng_->seed(s); }
     146
     147    ///
     148    /// @brief Set the seed using the /dev/urandom device.
     149    ///
     150    /// @return The seed acquired from /dev/urandom.
     151    ///
     152    /// @see seed, RNG::seed_from_devurandom, RNG::seed
     153    ///
     154    u_long seed_from_devurandom(void) { return rng_->seed_from_devurandom(); }
    131155
    132156    ///
     
    190214    /// @brief Default constructor.
    191215    ///
    192     DiscreteUniform(void) : range_(rng_->max()+1) {}
     216    DiscreteUniform(void) : range_(rng_->max()) {}
    193217
    194218    ///
    195219    /// @brief Constructor.
    196220    ///
    197     /// @param n sets the range. Object will generate integers from
    198     /// [0,n-1].
     221    /// The generator will generate integers from \f$ [0,n-1] \f$. If
     222    /// \a n is larger than the maximum number the random number
     223    /// generator can return, then (currently) \a n is adjusted
     224    /// appropriately.
     225    ///
     226    /// @todo If a too large \a n is given an exception should be
     227    /// thrown, i.e. the behaviour of this class will change. The case
     228    /// when argument is 0 is not treated gracefully (underlying GSL
     229    /// functionality will not return).
    199230    ///
    200231    DiscreteUniform(const u_long n) : range_(n)
    201     { if ( range_-1>rng_->max() ) range_=rng_->max()+1; }
     232    { if ( range_>rng_->max() ) range_=rng_->max(); }
    202233
    203234    ///
     
    281312
    282313    ///
     314    /// Set the seed to \a s in the underlying rng. If \a s is zero, a
     315    /// default value from the rng's original implementation is used
     316    /// (cf. GSL documentation).
     317    ///
     318    /// @brief Set the seed to \a s.
     319    ///
     320    /// @see seed_from_devurandom, RNG::seed_from_devurandom, RNG::seed
     321    ///
     322    inline void seed(u_long s) const { rng_->seed(s); }
     323
     324    ///
     325    /// @brief Set the seed using the /dev/urandom device.
     326    ///
     327    /// @return The seed acquired from /dev/urandom.
     328    ///
     329    /// @see seed, RNG::seed_from_devurandom, RNG::seed
     330    ///
     331    u_long seed_from_devurandom(void) { return rng_->seed_from_devurandom(); }
     332
     333    ///
    283334    /// @return A random number
    284335    ///
  • trunk/test/rnd_test.cc

    r399 r425  
    2121  bool ok = true;
    2222
    23   theplu::random::ContinuousUniform uniform;
    24 
    2523  theplu::random::RNG* rng=theplu::random::RNG::instance();
    26 
    27   // testing that minimal integer is zero for our generator
     24  // testing that minimal integer is zero for the generator
    2825  if (rng->min()){
    2926    *error << "Error: rng->min is not zero" << std::endl;
     
    3431    delete error;
    3532
    36   if (!ok)
    37     return -1;
    38   return 0;
     33  return (ok ? 0 : -1);
    3934}
Note: See TracChangeset for help on using the changeset viewer.