COIN-OR::LEMON - Graph Library

Changeset 2245:f09b1ea5c249 in lemon-0.x


Ignore:
Timestamp:
10/17/06 12:40:28 (14 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2995
Message:

Little modification on interface and documentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/random.h

    r2242 r2245  
    423423  /// The generator gives back random numbers of serveral types. To
    424424  /// get a random number from a range of a floating point type you
    425   /// can use one form of the \c operator(). If you want to get random
    426   /// number from a the {0, 1, ..., n-1} integer range use the \c
    427   /// operator[]. And to get random number from the whole range of an
    428   /// integer type you can use the \c integer() or \c uinteger()
    429   /// functions. After all you can get random bool with equal chance
    430   /// or given probability with the \c boolean() member function.
     425  /// can use one form of the \c operator() or the \c real() member
     426  /// function. If you want to get random number from the {0, 1, ...,
     427  /// n-1} integer range use the \c operator[] or the \c integer()
     428  /// method. And to get random number from the whole range of an
     429  /// integer type you can use the argumentless \c integer() or \c
     430  /// uinteger() functions. After all you can get random bool with
     431  /// equal chance of true and false or given probability of true
     432  /// result with the \c boolean() member functions.
    431433  ///
    432434  ///\code
    433   /// int a = rnd[100000];                  // 0..99999
     435  /// // The commented code is identical to the other
     436  /// double a = rnd();                     // [0.0, 1.0)
     437  /// // double a = rnd.real();             // [0.0, 1.0)
     438  /// double b = rnd(100.0);                // [0.0, 100.0)
     439  /// // double b = rnd.real(100.0);        // [0.0, 100.0)
     440  /// double c = rnd(1.0, 2.0);             // [1.0, 2.0)
     441  /// // double c = rnd.real(1.0, 2.0);     // [1.0, 2.0)
     442  /// int d = rnd[100000];                  // 0..99999
     443  /// // int d = rnd.integer(100000);       // 0..99999
     444  /// int e = rnd[6] + 1;                   // 1..6
     445  /// // int e = rnd.integer(1, 1 + 6);     // 1..6
    434446  /// int b = rnd.uinteger<int>();          // 0 .. 2^31 - 1
    435447  /// int c = rnd.integer<int>();           // - 2^31 .. 2^31 - 1
    436   /// double d = rnd();                     // [0.0, 1.0)
    437   /// double e = rnd(100.0);                // [0.0, 100.0)
    438   /// double f = rnd(1.0, 2.0);             // [1.0, 2.0)
    439448  /// bool g = rnd.boolean();               // P(g = true) = 0.5
    440449  /// bool h = rnd.boolean(0.8);            // P(h = true) = 0.8
    441450  ///\endcode
    442451  ///
    443   /// The lemon provides a global instance of the random number generator
    444   /// which name is \c rnd. Usually it is a good programming convenience
    445   /// to use this global generator to get random numbers.
     452  /// The lemon provides a global instance of the random number
     453  /// generator which name is \ref lemon::rnd "rnd". Usually it is a
     454  /// good programming convenience to use this global generator to get
     455  /// random numbers.
    446456  ///
    447457  /// \author Balazs Dezso
     
    449459  private:
    450460
    451 #if WORD_BIT == 32
    452     typedef unsigned int Word;
    453 #elif WORD_BIT == 64
     461    // architecture word
    454462    typedef unsigned long Word;
    455 #endif
    456463   
    457464    _random_bits::RandomCore<Word> core;
     
    487494    ///
    488495    /// Copy constructor. The generated sequence will be identical to
    489     /// the other sequence.
     496    /// the other sequence. It can be used to save the current state
     497    /// of the generator and later use it to generate the same
     498    /// sequence.
    490499    Random(const Random& other) {
    491500      core.copyState(other.core);
     
    495504    ///
    496505    /// Assign operator. The generated sequence will be identical to
    497     /// the other sequence.
     506    /// the other sequence. It can be used to save the current state
     507    /// of the generator and later use it to generate the same
     508    /// sequence.
    498509    Random& operator=(const Random& other) {
    499510      if (&other != this) {
     
    505516    /// \brief Returns a random real number
    506517    ///
    507     /// It returns a random real number from the range [0, 1). The default
    508     /// result type of this function is double.
    509     template <typename Number>
    510     Number operator()() {
     518    /// It returns a random real number from the range [0, 1). The
     519    /// default Number type is double.
     520    template <typename Number>
     521    Number real() {
    511522      return _random_bits::RealConversion<Number, Word>::convert(core);
    512523    }
    513524
     525    double real() {
     526      return real<double>();
     527    }
     528
     529    /// \brief Returns a random real number
     530    ///
     531    /// It returns a random real number from the range [0, b).
     532    template <typename Number>
     533    Number real(Number b) {
     534      return real<Number>() * b;
     535    }
     536
     537    /// \brief Returns a random real number
     538    ///
     539    /// It returns a random real number from the range [a, b).
     540    template <typename Number>
     541    Number real(Number a, Number b) {
     542      return real<Number>() * (b - a) + a;
     543    }
     544
     545    /// \brief Returns a random real number
     546    ///
     547    /// It returns a random double from the range [0, 1).
    514548    double operator()() {
    515       return operator()<double>();
     549      return real<double>();
    516550    }
    517551
     
    521555    template <typename Number>
    522556    Number operator()(Number b) {
    523       return operator()<Number>() * b;
     557      return real<Number>() * b;
    524558    }
    525559
     
    529563    template <typename Number>
    530564    Number operator()(Number a, Number b) {
    531       return operator()<Number>() * (b - a) + a;
     565      return real<Number>() * (b - a) + a;
    532566    }
    533567
    534568    /// \brief Returns a random integer from a range
    535569    ///
    536     /// It returns a random integer from the range {0, 1, ..., bound - 1}.
    537     template <typename Number>
    538     Number operator[](const Number& bound) {
    539       return _random_bits::Mapping<Number, Word>::map(core, bound);
     570    /// It returns a random integer from the range {0, 1, ..., b - 1}.
     571    template <typename Number>
     572    Number integer(Number b) {
     573      return _random_bits::Mapping<Number, Word>::map(core, b);
     574    }
     575
     576    /// \brief Returns a random integer from a range
     577    ///
     578    /// It returns a random integer from the range {a, a + 1, ..., b - 1}.
     579    template <typename Number>
     580    Number integer(Number a, Number b) {
     581      return _random_bits::Mapping<Number, Word>::map(core, b - a) + a;
     582    }
     583
     584    /// \brief Returns a random integer from a range
     585    ///
     586    /// It returns a random integer from the range {0, 1, ..., b - 1}.
     587    template <typename Number>
     588    Number operator[](Number b) {
     589      return _random_bits::Mapping<Number, Word>::map(core, b);
    540590    }
    541591
     
    587637
    588638
    589   /// \brief Global random number generator instance
    590   ///
    591   /// A global mersenne twister random number generator instance
    592639  extern Random rnd;
    593640
Note: See TracChangeset for help on using the changeset viewer.