| ... |
... |
@@ -66,10 +66,20 @@
|
| 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
|
| ... |
... |
@@ -526,6 +536,14 @@
|
| 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.
|
| ... |
... |
@@ -594,6 +612,73 @@
|
| 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
|
| ... |
... |
@@ -623,6 +708,12 @@
|
| 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).
|
| ... |
... |
@@ -680,6 +771,8 @@
|
| 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 |
}
|
| ... |
... |
@@ -709,6 +802,8 @@
|
| 709 |
802 |
return bool_producer.convert(core);
|
| 710 |
803 |
}
|
| 711 |
804 |
|
|
805 |
/// @}
|
|
806 |
|
| 712 |
807 |
///\name Non-uniform distributions
|
| 713 |
808 |
///
|
| 714 |
809 |
|