lemon/random.h
changeset 197 5893bacaa720
parent 177 b685e12e08c0
child 209 765619b7cbb2
equal deleted inserted replaced
13:36a7fcf2e8bc 14:0191b7b84be9
   538 
   538 
   539     ///\name Initialization
   539     ///\name Initialization
   540     ///
   540     ///
   541     /// @{
   541     /// @{
   542 
   542 
       
   543     ///\name Initialization
       
   544     ///
       
   545     /// @{
       
   546 
   543     /// \brief Default constructor
   547     /// \brief Default constructor
   544     ///
   548     ///
   545     /// Constructor with constant seeding.
   549     /// Constructor with constant seeding.
   546     Random() { core.initState(); }
   550     Random() { core.initState(); }
   547 
   551 
   609     }
   613     }
   610 
   614 
   611     /// \brief Seeding from file or from process id and time
   615     /// \brief Seeding from file or from process id and time
   612     ///
   616     ///
   613     /// By default, this function calls the \c seedFromFile() member
   617     /// By default, this function calls the \c seedFromFile() member
   614     /// function with the <tt>/dev/urandom</tt> file. If it is not success,
   618     /// function with the <tt>/dev/urandom</tt> file. If it does not success,
   615     /// it uses the \c seedFromTime().
   619     /// it uses the \c seedFromTime().
   616     /// \return Currently always true.
   620     /// \return Currently always true.
   617     bool seed() {
   621     bool seed() {
   618 #ifndef WIN32
   622 #ifndef WIN32
   619       if (seedFromFile("/dev/urandom", 0)) return true;
   623       if (seedFromFile("/dev/urandom", 0)) return true;
   632     /// source of randomness, while the <tt>urandom</tt> does not
   636     /// source of randomness, while the <tt>urandom</tt> does not
   633     /// block the input, but it could give back bytes with worse
   637     /// block the input, but it could give back bytes with worse
   634     /// entropy).
   638     /// entropy).
   635     /// \param file The source file
   639     /// \param file The source file
   636     /// \param offset The offset, from the file read.
   640     /// \param offset The offset, from the file read.
   637     /// \return True when the seeding is success.
   641     /// \return True when the seeding successes.
   638 #ifndef WIN32
   642 #ifndef WIN32
   639     bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0) 
   643     bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0) 
   640 #else
   644 #else
   641     bool seedFromFile(const std::string& file = "", int offset = 0) 
   645     bool seedFromFile(const std::string& file = "", int offset = 0) 
   642 #endif
   646 #endif
   702     template <typename Number>
   706     template <typename Number>
   703     Number real(Number a, Number b) { 
   707     Number real(Number a, Number b) { 
   704       return real<Number>() * (b - a) + a; 
   708       return real<Number>() * (b - a) + a; 
   705     }
   709     }
   706 
   710 
       
   711     /// @}
       
   712 
       
   713     ///\name Uniform distributions
       
   714     ///
       
   715     /// @{
       
   716 
   707     /// \brief Returns a random real number from the range [0, 1)
   717     /// \brief Returns a random real number from the range [0, 1)
   708     ///
   718     ///
   709     /// It returns a random double from the range [0, 1).
   719     /// It returns a random double from the range [0, 1).
   710     double operator()() {
   720     double operator()() {
   711       return real<double>();
   721       return real<double>();
   758     /// type of this function is <tt>unsigned int</tt>.
   768     /// type of this function is <tt>unsigned int</tt>.
   759     template <typename Number>
   769     template <typename Number>
   760     Number uinteger() {
   770     Number uinteger() {
   761       return _random_bits::IntConversion<Number, Word>::convert(core);
   771       return _random_bits::IntConversion<Number, Word>::convert(core);
   762     }
   772     }
       
   773 
       
   774     /// @}
   763 
   775 
   764     unsigned int uinteger() {
   776     unsigned int uinteger() {
   765       return uinteger<unsigned int>();
   777       return uinteger<unsigned int>();
   766     }
   778     }
   767 
   779