| ... |
... |
@@ -63,16 +63,26 @@
|
| 63 |
63 |
#define LEMON_RANDOM_H
|
| 64 |
64 |
|
| 65 |
65 |
#include <algorithm>
|
| 66 |
66 |
#include <iterator>
|
| 67 |
67 |
#include <vector>
|
| 68 |
68 |
#include <limits>
|
|
69 |
#include <fstream>
|
| 69 |
70 |
|
| 70 |
71 |
#include <lemon/math.h>
|
| 71 |
72 |
#include <lemon/dim2.h>
|
| 72 |
73 |
|
|
74 |
#ifndef WIN32
|
|
75 |
#include <sys/time.h>
|
|
76 |
#include <ctime>
|
|
77 |
#include <sys/types.h>
|
|
78 |
#include <unistd.h>
|
|
79 |
#else
|
|
80 |
#include <windows.h>
|
|
81 |
#endif
|
|
82 |
|
| 73 |
83 |
///\ingroup misc
|
| 74 |
84 |
///\file
|
| 75 |
85 |
///\brief Mersenne Twister random number generator
|
| 76 |
86 |
|
| 77 |
87 |
namespace lemon {
|
| 78 |
88 |
|
| ... |
... |
@@ -523,12 +533,20 @@
|
| 523 |
533 |
_random_bits::RandomCore<Word> core;
|
| 524 |
534 |
_random_bits::BoolProducer<Word> bool_producer;
|
| 525 |
535 |
|
| 526 |
536 |
|
| 527 |
537 |
public:
|
| 528 |
538 |
|
|
539 |
///\name Initialization
|
|
540 |
///
|
|
541 |
/// @{
|
|
542 |
|
|
543 |
///\name Initialization
|
|
544 |
///
|
|
545 |
/// @{
|
|
546 |
|
| 529 |
547 |
/// \brief Default constructor
|
| 530 |
548 |
///
|
| 531 |
549 |
/// Constructor with constant seeding.
|
| 532 |
550 |
Random() { core.initState(); }
|
| 533 |
551 |
|
| 534 |
552 |
/// \brief Constructor with seed
|
| ... |
... |
@@ -591,12 +609,79 @@
|
| 591 |
609 |
template <typename Iterator>
|
| 592 |
610 |
void seed(Iterator begin, Iterator end) {
|
| 593 |
611 |
typedef typename std::iterator_traits<Iterator>::value_type Number;
|
| 594 |
612 |
_random_bits::Initializer<Number, Word>::init(core, begin, end);
|
| 595 |
613 |
}
|
| 596 |
614 |
|
|
615 |
/// \brief Seeding from file or from process id and time
|
|
616 |
///
|
|
617 |
/// By default, this function calls the \c seedFromFile() member
|
|
618 |
/// function with the <tt>/dev/urandom</tt> file. If it does not success,
|
|
619 |
/// it uses the \c seedFromTime().
|
|
620 |
/// \return Currently always true.
|
|
621 |
bool seed() {
|
|
622 |
#ifndef WIN32
|
|
623 |
if (seedFromFile("/dev/urandom", 0)) return true;
|
|
624 |
#endif
|
|
625 |
if (seedFromTime()) return true;
|
|
626 |
return false;
|
|
627 |
}
|
|
628 |
|
|
629 |
/// \brief Seeding from file
|
|
630 |
///
|
|
631 |
/// Seeding the random sequence from file. The linux kernel has two
|
|
632 |
/// devices, <tt>/dev/random</tt> and <tt>/dev/urandom</tt> which
|
|
633 |
/// could give good seed values for pseudo random generators (The
|
|
634 |
/// difference between two devices is that the <tt>random</tt> may
|
|
635 |
/// block the reading operation while the kernel can give good
|
|
636 |
/// source of randomness, while the <tt>urandom</tt> does not
|
|
637 |
/// block the input, but it could give back bytes with worse
|
|
638 |
/// entropy).
|
|
639 |
/// \param file The source file
|
|
640 |
/// \param offset The offset, from the file read.
|
|
641 |
/// \return True when the seeding successes.
|
|
642 |
#ifndef WIN32
|
|
643 |
bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0)
|
|
644 |
#else
|
|
645 |
bool seedFromFile(const std::string& file = "", int offset = 0)
|
|
646 |
#endif
|
|
647 |
{
|
|
648 |
std::ifstream rs(file.c_str());
|
|
649 |
const int size = 4;
|
|
650 |
Word buf[size];
|
|
651 |
if (offset != 0 && !rs.seekg(offset)) return false;
|
|
652 |
if (!rs.read(reinterpret_cast<char*>(buf), sizeof(buf))) return false;
|
|
653 |
seed(buf, buf + size);
|
|
654 |
return true;
|
|
655 |
}
|
|
656 |
|
|
657 |
/// \brief Seding from process id and time
|
|
658 |
///
|
|
659 |
/// Seding from process id and time. This function uses the
|
|
660 |
/// current process id and the current time for initialize the
|
|
661 |
/// random sequence.
|
|
662 |
/// \return Currently always true.
|
|
663 |
bool seedFromTime() {
|
|
664 |
#ifndef WIN32
|
|
665 |
timeval tv;
|
|
666 |
gettimeofday(&tv, 0);
|
|
667 |
seed(getpid() + tv.tv_sec + tv.tv_usec);
|
|
668 |
#else
|
|
669 |
FILETIME time;
|
|
670 |
GetSystemTimeAsFileTime(&time);
|
|
671 |
seed(GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime);
|
|
672 |
#endif
|
|
673 |
return true;
|
|
674 |
}
|
|
675 |
|
|
676 |
/// @}
|
|
677 |
|
|
678 |
///\name Uniform distributions
|
|
679 |
///
|
|
680 |
/// @{
|
|
681 |
|
| 597 |
682 |
/// \brief Returns a random real number from the range [0, 1)
|
| 598 |
683 |
///
|
| 599 |
684 |
/// It returns a random real number from the range [0, 1). The
|
| 600 |
685 |
/// default Number type is \c double.
|
| 601 |
686 |
template <typename Number>
|
| 602 |
687 |
Number real() {
|
| ... |
... |
@@ -620,12 +705,18 @@
|
| 620 |
705 |
/// It returns a random real number from the range [a, b).
|
| 621 |
706 |
template <typename Number>
|
| 622 |
707 |
Number real(Number a, Number b) {
|
| 623 |
708 |
return real<Number>() * (b - a) + a;
|
| 624 |
709 |
}
|
| 625 |
710 |
|
|
711 |
/// @}
|
|
712 |
|
|
713 |
///\name Uniform distributions
|
|
714 |
///
|
|
715 |
/// @{
|
|
716 |
|
| 626 |
717 |
/// \brief Returns a random real number from the range [0, 1)
|
| 627 |
718 |
///
|
| 628 |
719 |
/// It returns a random double from the range [0, 1).
|
| 629 |
720 |
double operator()() {
|
| 630 |
721 |
return real<double>();
|
| 631 |
722 |
}
|
| ... |
... |
@@ -677,12 +768,14 @@
|
| 677 |
768 |
/// type of this function is <tt>unsigned int</tt>.
|
| 678 |
769 |
template <typename Number>
|
| 679 |
770 |
Number uinteger() {
|
| 680 |
771 |
return _random_bits::IntConversion<Number, Word>::convert(core);
|
| 681 |
772 |
}
|
| 682 |
773 |
|
|
774 |
/// @}
|
|
775 |
|
| 683 |
776 |
unsigned int uinteger() {
|
| 684 |
777 |
return uinteger<unsigned int>();
|
| 685 |
778 |
}
|
| 686 |
779 |
|
| 687 |
780 |
/// \brief Returns a random integer
|
| 688 |
781 |
///
|
| ... |
... |
@@ -706,12 +799,14 @@
|
| 706 |
799 |
/// random bits. Every time when it become empty the generator makes
|
| 707 |
800 |
/// a new random word and fill the buffer up.
|
| 708 |
801 |
bool boolean() {
|
| 709 |
802 |
return bool_producer.convert(core);
|
| 710 |
803 |
}
|
| 711 |
804 |
|
|
805 |
/// @}
|
|
806 |
|
| 712 |
807 |
///\name Non-uniform distributions
|
| 713 |
808 |
///
|
| 714 |
809 |
|
| 715 |
810 |
///@{
|
| 716 |
811 |
|
| 717 |
812 |
/// \brief Returns a random bool
|