Changeset 2245:f09b1ea5c249 in lemon0.x
 Timestamp:
 10/17/06 12:40:28 (18 years ago)
 Branch:
 default
 Phase:
 public
 Convert:
 svn:c9d7d8f590d60310b91f818b3a526b0e/lemon/trunk@2995
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

lemon/random.h
r2242 r2245 423 423 /// The generator gives back random numbers of serveral types. To 424 424 /// 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, ..., n1} 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 /// n1} 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. 431 433 /// 432 434 ///\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 434 446 /// int b = rnd.uinteger<int>(); // 0 .. 2^31  1 435 447 /// 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)439 448 /// bool g = rnd.boolean(); // P(g = true) = 0.5 440 449 /// bool h = rnd.boolean(0.8); // P(h = true) = 0.8 441 450 ///\endcode 442 451 /// 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. 446 456 /// 447 457 /// \author Balazs Dezso … … 449 459 private: 450 460 451 #if WORD_BIT == 32 452 typedef unsigned int Word; 453 #elif WORD_BIT == 64 461 // architecture word 454 462 typedef unsigned long Word; 455 #endif456 463 457 464 _random_bits::RandomCore<Word> core; … … 487 494 /// 488 495 /// 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. 490 499 Random(const Random& other) { 491 500 core.copyState(other.core); … … 495 504 /// 496 505 /// 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. 498 509 Random& operator=(const Random& other) { 499 510 if (&other != this) { … … 505 516 /// \brief Returns a random real number 506 517 /// 507 /// It returns a random real number from the range [0, 1). The default508 /// result type of this functionis 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() { 511 522 return _random_bits::RealConversion<Number, Word>::convert(core); 512 523 } 513 524 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). 514 548 double operator()() { 515 return operator()<double>();549 return real<double>(); 516 550 } 517 551 … … 521 555 template <typename Number> 522 556 Number operator()(Number b) { 523 return operator()<Number>() * b;557 return real<Number>() * b; 524 558 } 525 559 … … 529 563 template <typename Number> 530 564 Number operator()(Number a, Number b) { 531 return operator()<Number>() * (b  a) + a;565 return real<Number>() * (b  a) + a; 532 566 } 533 567 534 568 /// \brief Returns a random integer from a range 535 569 /// 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); 540 590 } 541 591 … … 587 637 588 638 589 /// \brief Global random number generator instance590 ///591 /// A global mersenne twister random number generator instance592 639 extern Random rnd; 593 640
Note: See TracChangeset
for help on using the changeset viewer.