lemon/random.h
changeset 51 90201bb15a8d
parent 39 0a01d811071f
child 62 4790635473ef
equal deleted inserted replaced
5:d76f570281fc 6:c481e6dbd91e
   508   /// int c = rnd.integer<int>();           // - 2^31 .. 2^31 - 1
   508   /// int c = rnd.integer<int>();           // - 2^31 .. 2^31 - 1
   509   /// bool g = rnd.boolean();               // P(g = true) = 0.5
   509   /// bool g = rnd.boolean();               // P(g = true) = 0.5
   510   /// bool h = rnd.boolean(0.8);            // P(h = true) = 0.8
   510   /// bool h = rnd.boolean(0.8);            // P(h = true) = 0.8
   511   ///\endcode
   511   ///\endcode
   512   ///
   512   ///
   513   /// The lemon provides a global instance of the random number
   513   /// LEMON provides a global instance of the random number
   514   /// generator which name is \ref lemon::rnd "rnd". Usually it is a
   514   /// generator which name is \ref lemon::rnd "rnd". Usually it is a
   515   /// good programming convenience to use this global generator to get
   515   /// good programming convenience to use this global generator to get
   516   /// random numbers.
   516   /// random numbers.
   517   class Random {
   517   class Random {
   518   private:
   518   private:
   524     _random_bits::BoolProducer<Word> bool_producer;
   524     _random_bits::BoolProducer<Word> bool_producer;
   525     
   525     
   526 
   526 
   527   public:
   527   public:
   528 
   528 
   529     /// \brief Constructor
   529     /// \brief Default constructor
   530     ///
   530     ///
   531     /// Constructor with constant seeding.
   531     /// Constructor with constant seeding.
   532     Random() { core.initState(); }
   532     Random() { core.initState(); }
   533 
   533 
   534     /// \brief Constructor
   534     /// \brief Constructor with seed
   535     ///
   535     ///
   536     /// Constructor with seed. The current number type will be converted
   536     /// Constructor with seed. The current number type will be converted
   537     /// to the architecture word type.
   537     /// to the architecture word type.
   538     template <typename Number>
   538     template <typename Number>
   539     Random(Number seed) { 
   539     Random(Number seed) { 
   540       _random_bits::Initializer<Number, Word>::init(core, seed);
   540       _random_bits::Initializer<Number, Word>::init(core, seed);
   541     }
   541     }
   542 
   542 
   543     /// \brief Constructor
   543     /// \brief Constructor with array seeding
   544     ///
   544     ///
   545     /// Constructor with array seeding. The given range should contain
   545     /// Constructor with array seeding. The given range should contain
   546     /// any number type and the numbers will be converted to the
   546     /// any number type and the numbers will be converted to the
   547     /// architecture word type.
   547     /// architecture word type.
   548     template <typename Iterator>
   548     template <typename Iterator>
   575     }
   575     }
   576 
   576 
   577     /// \brief Returns a random real number from the range [0, 1)
   577     /// \brief Returns a random real number from the range [0, 1)
   578     ///
   578     ///
   579     /// It returns a random real number from the range [0, 1). The
   579     /// It returns a random real number from the range [0, 1). The
   580     /// default Number type is double.
   580     /// default Number type is \c double.
   581     template <typename Number>
   581     template <typename Number>
   582     Number real() {
   582     Number real() {
   583       return _random_bits::RealConversion<Number, Word>::convert(core);
   583       return _random_bits::RealConversion<Number, Word>::convert(core);
   584     }
   584     }
   585 
   585 
   651     }
   651     }
   652 
   652 
   653     /// \brief Returns a random non-negative integer
   653     /// \brief Returns a random non-negative integer
   654     ///
   654     ///
   655     /// It returns a random non-negative integer uniformly from the
   655     /// It returns a random non-negative integer uniformly from the
   656     /// whole range of the current \c Number type.  The default result
   656     /// whole range of the current \c Number type. The default result
   657     /// type of this function is unsigned int.
   657     /// type of this function is <tt>unsigned int</tt>.
   658     template <typename Number>
   658     template <typename Number>
   659     Number uinteger() {
   659     Number uinteger() {
   660       return _random_bits::IntConversion<Number, Word>::convert(core);
   660       return _random_bits::IntConversion<Number, Word>::convert(core);
   661     }
   661     }
   662 
   662 
   666 
   666 
   667     /// \brief Returns a random integer
   667     /// \brief Returns a random integer
   668     ///
   668     ///
   669     /// It returns a random integer uniformly from the whole range of
   669     /// It returns a random integer uniformly from the whole range of
   670     /// the current \c Number type. The default result type of this
   670     /// the current \c Number type. The default result type of this
   671     /// function is int.
   671     /// function is \c int.
   672     template <typename Number>
   672     template <typename Number>
   673     Number integer() {
   673     Number integer() {
   674       static const int nb = std::numeric_limits<Number>::digits + 
   674       static const int nb = std::numeric_limits<Number>::digits + 
   675         (std::numeric_limits<Number>::is_signed ? 1 : 0);
   675         (std::numeric_limits<Number>::is_signed ? 1 : 0);
   676       return _random_bits::IntConversion<Number, Word, nb>::convert(core);
   676       return _random_bits::IntConversion<Number, Word, nb>::convert(core);
   687     /// a new random word and fill the buffer up.
   687     /// a new random word and fill the buffer up.
   688     bool boolean() {
   688     bool boolean() {
   689       return bool_producer.convert(core);
   689       return bool_producer.convert(core);
   690     }
   690     }
   691 
   691 
   692     ///\name Nonuniform distributions
   692     ///\name Non-uniform distributions
   693     ///
   693     ///
   694     
   694     
   695     ///@{
   695     ///@{
   696     
   696     
   697     /// \brief Returns a random bool
   697     /// \brief Returns a random bool