420 /// initialization and generation phase so they produce two |
420 /// initialization and generation phase so they produce two |
421 /// completly different sequences. |
421 /// completly different sequences. |
422 /// |
422 /// |
423 /// The generator gives back random numbers of serveral types. To |
423 /// The generator gives back random numbers of serveral types. To |
424 /// get a random number from a range of a floating point type you |
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 |
425 /// can use one form of the \c operator() or the \c real() member |
426 /// number from a the {0, 1, ..., n-1} integer range use the \c |
426 /// function. If you want to get random number from the {0, 1, ..., |
427 /// operator[]. And to get random number from the whole range of an |
427 /// n-1} integer range use the \c operator[] or the \c integer() |
428 /// integer type you can use the \c integer() or \c uinteger() |
428 /// method. And to get random number from the whole range of an |
429 /// functions. After all you can get random bool with equal chance |
429 /// integer type you can use the argumentless \c integer() or \c |
430 /// or given probability with the \c boolean() member function. |
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 ///\code |
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 /// int b = rnd.uinteger<int>(); // 0 .. 2^31 - 1 |
446 /// int b = rnd.uinteger<int>(); // 0 .. 2^31 - 1 |
435 /// int c = rnd.integer<int>(); // - 2^31 .. 2^31 - 1 |
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 /// bool g = rnd.boolean(); // P(g = true) = 0.5 |
448 /// bool g = rnd.boolean(); // P(g = true) = 0.5 |
440 /// bool h = rnd.boolean(0.8); // P(h = true) = 0.8 |
449 /// bool h = rnd.boolean(0.8); // P(h = true) = 0.8 |
441 ///\endcode |
450 ///\endcode |
442 /// |
451 /// |
443 /// The lemon provides a global instance of the random number generator |
452 /// The lemon provides a global instance of the random number |
444 /// which name is \c rnd. Usually it is a good programming convenience |
453 /// generator which name is \ref lemon::rnd "rnd". Usually it is a |
445 /// to use this global generator to get random numbers. |
454 /// good programming convenience to use this global generator to get |
|
455 /// random numbers. |
446 /// |
456 /// |
447 /// \author Balazs Dezso |
457 /// \author Balazs Dezso |
448 class Random { |
458 class Random { |
449 private: |
459 private: |
450 |
460 |
451 #if WORD_BIT == 32 |
461 // architecture word |
452 typedef unsigned int Word; |
|
453 #elif WORD_BIT == 64 |
|
454 typedef unsigned long Word; |
462 typedef unsigned long Word; |
455 #endif |
|
456 |
463 |
457 _random_bits::RandomCore<Word> core; |
464 _random_bits::RandomCore<Word> core; |
458 |
465 |
459 public: |
466 public: |
460 |
467 |
484 } |
491 } |
485 |
492 |
486 /// \brief Copy constructor |
493 /// \brief Copy constructor |
487 /// |
494 /// |
488 /// Copy constructor. The generated sequence will be identical to |
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 Random(const Random& other) { |
499 Random(const Random& other) { |
491 core.copyState(other.core); |
500 core.copyState(other.core); |
492 } |
501 } |
493 |
502 |
494 /// \brief Assign operator |
503 /// \brief Assign operator |
495 /// |
504 /// |
496 /// Assign operator. The generated sequence will be identical to |
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 Random& operator=(const Random& other) { |
509 Random& operator=(const Random& other) { |
499 if (&other != this) { |
510 if (&other != this) { |
500 core.copyState(other.core); |
511 core.copyState(other.core); |
501 } |
512 } |
502 return *this; |
513 return *this; |
503 } |
514 } |
504 |
515 |
505 /// \brief Returns a random real number |
516 /// \brief Returns a random real number |
506 /// |
517 /// |
507 /// It returns a random real number from the range [0, 1). The default |
518 /// It returns a random real number from the range [0, 1). The |
508 /// result type of this function is double. |
519 /// default Number type is double. |
509 template <typename Number> |
520 template <typename Number> |
510 Number operator()() { |
521 Number real() { |
511 return _random_bits::RealConversion<Number, Word>::convert(core); |
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 double operator()() { |
548 double operator()() { |
515 return operator()<double>(); |
549 return real<double>(); |
516 } |
550 } |
517 |
551 |
518 /// \brief Returns a random real number |
552 /// \brief Returns a random real number |
519 /// |
553 /// |
520 /// It returns a random real number from the range [0, b). |
554 /// It returns a random real number from the range [0, b). |
521 template <typename Number> |
555 template <typename Number> |
522 Number operator()(Number b) { |
556 Number operator()(Number b) { |
523 return operator()<Number>() * b; |
557 return real<Number>() * b; |
524 } |
558 } |
525 |
559 |
526 /// \brief Returns a random real number |
560 /// \brief Returns a random real number |
527 /// |
561 /// |
528 /// It returns a random real number from the range [a, b). |
562 /// It returns a random real number from the range [a, b). |
529 template <typename Number> |
563 template <typename Number> |
530 Number operator()(Number a, Number b) { |
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 /// \brief Returns a random integer from a range |
568 /// \brief Returns a random integer from a range |
535 /// |
569 /// |
536 /// It returns a random integer from the range {0, 1, ..., bound - 1}. |
570 /// It returns a random integer from the range {0, 1, ..., b - 1}. |
537 template <typename Number> |
571 template <typename Number> |
538 Number operator[](const Number& bound) { |
572 Number integer(Number b) { |
539 return _random_bits::Mapping<Number, Word>::map(core, bound); |
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 |
542 /// \brief Returns a random non-negative integer |
592 /// \brief Returns a random non-negative integer |
543 /// |
593 /// |
544 /// It returns a random non-negative integer uniformly from the |
594 /// It returns a random non-negative integer uniformly from the |