Little modification on interface and documentation
authordeba
Tue, 17 Oct 2006 10:40:28 +0000
changeset 2245f09b1ea5c249
parent 2244 a28b4e0aa787
child 2246 9c472eee236f
Little modification on interface and documentation
lemon/random.h
     1.1 --- a/lemon/random.h	Tue Oct 17 10:32:12 2006 +0000
     1.2 +++ b/lemon/random.h	Tue Oct 17 10:40:28 2006 +0000
     1.3 @@ -422,37 +422,44 @@
     1.4    ///
     1.5    /// The generator gives back random numbers of serveral types. To
     1.6    /// get a random number from a range of a floating point type you
     1.7 -  /// can use one form of the \c operator(). If you want to get random
     1.8 -  /// number from a the {0, 1, ..., n-1} integer range use the \c
     1.9 -  /// operator[]. And to get random number from the whole range of an
    1.10 -  /// integer type you can use the \c integer() or \c uinteger()
    1.11 -  /// functions. After all you can get random bool with equal chance
    1.12 -  /// or given probability with the \c boolean() member function.
    1.13 +  /// can use one form of the \c operator() or the \c real() member
    1.14 +  /// function. If you want to get random number from the {0, 1, ...,
    1.15 +  /// n-1} integer range use the \c operator[] or the \c integer()
    1.16 +  /// method. And to get random number from the whole range of an
    1.17 +  /// integer type you can use the argumentless \c integer() or \c
    1.18 +  /// uinteger() functions. After all you can get random bool with
    1.19 +  /// equal chance of true and false or given probability of true
    1.20 +  /// result with the \c boolean() member functions.
    1.21    ///
    1.22    ///\code
    1.23 -  /// int a = rnd[100000];                  // 0..99999
    1.24 +  /// // The commented code is identical to the other
    1.25 +  /// double a = rnd();                     // [0.0, 1.0)
    1.26 +  /// // double a = rnd.real();             // [0.0, 1.0)
    1.27 +  /// double b = rnd(100.0);                // [0.0, 100.0)
    1.28 +  /// // double b = rnd.real(100.0);        // [0.0, 100.0)
    1.29 +  /// double c = rnd(1.0, 2.0);             // [1.0, 2.0)
    1.30 +  /// // double c = rnd.real(1.0, 2.0);     // [1.0, 2.0)
    1.31 +  /// int d = rnd[100000];                  // 0..99999
    1.32 +  /// // int d = rnd.integer(100000);       // 0..99999
    1.33 +  /// int e = rnd[6] + 1;                   // 1..6
    1.34 +  /// // int e = rnd.integer(1, 1 + 6);     // 1..6
    1.35    /// int b = rnd.uinteger<int>();          // 0 .. 2^31 - 1
    1.36    /// int c = rnd.integer<int>();           // - 2^31 .. 2^31 - 1
    1.37 -  /// double d = rnd();                     // [0.0, 1.0)
    1.38 -  /// double e = rnd(100.0);                // [0.0, 100.0)
    1.39 -  /// double f = rnd(1.0, 2.0);             // [1.0, 2.0)
    1.40    /// bool g = rnd.boolean();               // P(g = true) = 0.5
    1.41    /// bool h = rnd.boolean(0.8);            // P(h = true) = 0.8
    1.42    ///\endcode
    1.43    ///
    1.44 -  /// The lemon provides a global instance of the random number generator
    1.45 -  /// which name is \c rnd. Usually it is a good programming convenience
    1.46 -  /// to use this global generator to get random numbers.
    1.47 +  /// The lemon provides a global instance of the random number
    1.48 +  /// generator which name is \ref lemon::rnd "rnd". Usually it is a
    1.49 +  /// good programming convenience to use this global generator to get
    1.50 +  /// random numbers.
    1.51    ///
    1.52    /// \author Balazs Dezso
    1.53    class Random {
    1.54    private:
    1.55  
    1.56 -#if WORD_BIT == 32
    1.57 -    typedef unsigned int Word;
    1.58 -#elif WORD_BIT == 64
    1.59 +    // architecture word
    1.60      typedef unsigned long Word;
    1.61 -#endif
    1.62      
    1.63      _random_bits::RandomCore<Word> core;
    1.64  
    1.65 @@ -486,7 +493,9 @@
    1.66      /// \brief Copy constructor
    1.67      ///
    1.68      /// Copy constructor. The generated sequence will be identical to
    1.69 -    /// the other sequence.
    1.70 +    /// the other sequence. It can be used to save the current state
    1.71 +    /// of the generator and later use it to generate the same
    1.72 +    /// sequence.
    1.73      Random(const Random& other) {
    1.74        core.copyState(other.core);
    1.75      }
    1.76 @@ -494,7 +503,9 @@
    1.77      /// \brief Assign operator
    1.78      ///
    1.79      /// Assign operator. The generated sequence will be identical to
    1.80 -    /// the other sequence.
    1.81 +    /// the other sequence. It can be used to save the current state
    1.82 +    /// of the generator and later use it to generate the same
    1.83 +    /// sequence.
    1.84      Random& operator=(const Random& other) {
    1.85        if (&other != this) {
    1.86          core.copyState(other.core);
    1.87 @@ -504,15 +515,38 @@
    1.88  
    1.89      /// \brief Returns a random real number
    1.90      ///
    1.91 -    /// It returns a random real number from the range [0, 1). The default
    1.92 -    /// result type of this function is double.
    1.93 +    /// It returns a random real number from the range [0, 1). The
    1.94 +    /// default Number type is double.
    1.95      template <typename Number>
    1.96 -    Number operator()() {
    1.97 +    Number real() {
    1.98        return _random_bits::RealConversion<Number, Word>::convert(core);
    1.99      }
   1.100  
   1.101 +    double real() {
   1.102 +      return real<double>();
   1.103 +    }
   1.104 +
   1.105 +    /// \brief Returns a random real number
   1.106 +    ///
   1.107 +    /// It returns a random real number from the range [0, b).
   1.108 +    template <typename Number>
   1.109 +    Number real(Number b) { 
   1.110 +      return real<Number>() * b; 
   1.111 +    }
   1.112 +
   1.113 +    /// \brief Returns a random real number
   1.114 +    ///
   1.115 +    /// It returns a random real number from the range [a, b).
   1.116 +    template <typename Number>
   1.117 +    Number real(Number a, Number b) { 
   1.118 +      return real<Number>() * (b - a) + a; 
   1.119 +    }
   1.120 +
   1.121 +    /// \brief Returns a random real number
   1.122 +    ///
   1.123 +    /// It returns a random double from the range [0, 1).
   1.124      double operator()() {
   1.125 -      return operator()<double>();
   1.126 +      return real<double>();
   1.127      }
   1.128  
   1.129      /// \brief Returns a random real number
   1.130 @@ -520,7 +554,7 @@
   1.131      /// It returns a random real number from the range [0, b).
   1.132      template <typename Number>
   1.133      Number operator()(Number b) { 
   1.134 -      return operator()<Number>() * b; 
   1.135 +      return real<Number>() * b; 
   1.136      }
   1.137  
   1.138      /// \brief Returns a random real number
   1.139 @@ -528,15 +562,31 @@
   1.140      /// It returns a random real number from the range [a, b).
   1.141      template <typename Number>
   1.142      Number operator()(Number a, Number b) { 
   1.143 -      return operator()<Number>() * (b - a) + a; 
   1.144 +      return real<Number>() * (b - a) + a; 
   1.145      }
   1.146  
   1.147      /// \brief Returns a random integer from a range
   1.148      ///
   1.149 -    /// It returns a random integer from the range {0, 1, ..., bound - 1}.
   1.150 +    /// It returns a random integer from the range {0, 1, ..., b - 1}.
   1.151      template <typename Number>
   1.152 -    Number operator[](const Number& bound) {
   1.153 -      return _random_bits::Mapping<Number, Word>::map(core, bound);
   1.154 +    Number integer(Number b) {
   1.155 +      return _random_bits::Mapping<Number, Word>::map(core, b);
   1.156 +    }
   1.157 +
   1.158 +    /// \brief Returns a random integer from a range
   1.159 +    ///
   1.160 +    /// It returns a random integer from the range {a, a + 1, ..., b - 1}.
   1.161 +    template <typename Number>
   1.162 +    Number integer(Number a, Number b) {
   1.163 +      return _random_bits::Mapping<Number, Word>::map(core, b - a) + a;
   1.164 +    }
   1.165 +
   1.166 +    /// \brief Returns a random integer from a range
   1.167 +    ///
   1.168 +    /// It returns a random integer from the range {0, 1, ..., b - 1}.
   1.169 +    template <typename Number>
   1.170 +    Number operator[](Number b) {
   1.171 +      return _random_bits::Mapping<Number, Word>::map(core, b);
   1.172      }
   1.173  
   1.174      /// \brief Returns a random non-negative integer
   1.175 @@ -586,9 +636,6 @@
   1.176    };
   1.177  
   1.178  
   1.179 -  /// \brief Global random number generator instance
   1.180 -  ///
   1.181 -  /// A global mersenne twister random number generator instance
   1.182    extern Random rnd;
   1.183  
   1.184  }