| ... | ... |
@@ -531,24 +531,28 @@ |
| 531 | 531 |
typedef unsigned long Word; |
| 532 | 532 |
|
| 533 | 533 |
_random_bits::RandomCore<Word> core; |
| 534 | 534 |
_random_bits::BoolProducer<Word> bool_producer; |
| 535 | 535 |
|
| 536 | 536 |
|
| 537 | 537 |
public: |
| 538 | 538 |
|
| 539 | 539 |
///\name Initialization |
| 540 | 540 |
/// |
| 541 | 541 |
/// @{
|
| 542 | 542 |
|
| 543 |
///\name Initialization |
|
| 544 |
/// |
|
| 545 |
/// @{
|
|
| 546 |
|
|
| 543 | 547 |
/// \brief Default constructor |
| 544 | 548 |
/// |
| 545 | 549 |
/// Constructor with constant seeding. |
| 546 | 550 |
Random() { core.initState(); }
|
| 547 | 551 |
|
| 548 | 552 |
/// \brief Constructor with seed |
| 549 | 553 |
/// |
| 550 | 554 |
/// Constructor with seed. The current number type will be converted |
| 551 | 555 |
/// to the architecture word type. |
| 552 | 556 |
template <typename Number> |
| 553 | 557 |
Random(Number seed) {
|
| 554 | 558 |
_random_bits::Initializer<Number, Word>::init(core, seed); |
| ... | ... |
@@ -602,48 +606,48 @@ |
| 602 | 606 |
/// Seeding the random sequence. The given range should contain |
| 603 | 607 |
/// any number type and the numbers will be converted to the |
| 604 | 608 |
/// architecture word type. |
| 605 | 609 |
template <typename Iterator> |
| 606 | 610 |
void seed(Iterator begin, Iterator end) {
|
| 607 | 611 |
typedef typename std::iterator_traits<Iterator>::value_type Number; |
| 608 | 612 |
_random_bits::Initializer<Number, Word>::init(core, begin, end); |
| 609 | 613 |
} |
| 610 | 614 |
|
| 611 | 615 |
/// \brief Seeding from file or from process id and time |
| 612 | 616 |
/// |
| 613 | 617 |
/// By default, this function calls the \c seedFromFile() member |
| 614 |
/// function with the <tt>/dev/urandom</tt> file. If it |
|
| 618 |
/// function with the <tt>/dev/urandom</tt> file. If it does not success, |
|
| 615 | 619 |
/// it uses the \c seedFromTime(). |
| 616 | 620 |
/// \return Currently always true. |
| 617 | 621 |
bool seed() {
|
| 618 | 622 |
#ifndef WIN32 |
| 619 | 623 |
if (seedFromFile("/dev/urandom", 0)) return true;
|
| 620 | 624 |
#endif |
| 621 | 625 |
if (seedFromTime()) return true; |
| 622 | 626 |
return false; |
| 623 | 627 |
} |
| 624 | 628 |
|
| 625 | 629 |
/// \brief Seeding from file |
| 626 | 630 |
/// |
| 627 | 631 |
/// Seeding the random sequence from file. The linux kernel has two |
| 628 | 632 |
/// devices, <tt>/dev/random</tt> and <tt>/dev/urandom</tt> which |
| 629 | 633 |
/// could give good seed values for pseudo random generators (The |
| 630 | 634 |
/// difference between two devices is that the <tt>random</tt> may |
| 631 | 635 |
/// block the reading operation while the kernel can give good |
| 632 | 636 |
/// source of randomness, while the <tt>urandom</tt> does not |
| 633 | 637 |
/// block the input, but it could give back bytes with worse |
| 634 | 638 |
/// entropy). |
| 635 | 639 |
/// \param file The source file |
| 636 | 640 |
/// \param offset The offset, from the file read. |
| 637 |
/// \return True when the seeding |
|
| 641 |
/// \return True when the seeding successes. |
|
| 638 | 642 |
#ifndef WIN32 |
| 639 | 643 |
bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0) |
| 640 | 644 |
#else |
| 641 | 645 |
bool seedFromFile(const std::string& file = "", int offset = 0) |
| 642 | 646 |
#endif |
| 643 | 647 |
{
|
| 644 | 648 |
std::ifstream rs(file.c_str()); |
| 645 | 649 |
const int size = 4; |
| 646 | 650 |
Word buf[size]; |
| 647 | 651 |
if (offset != 0 && !rs.seekg(offset)) return false; |
| 648 | 652 |
if (!rs.read(reinterpret_cast<char*>(buf), sizeof(buf))) return false; |
| 649 | 653 |
seed(buf, buf + size); |
| ... | ... |
@@ -695,24 +699,30 @@ |
| 695 | 699 |
Number real(Number b) {
|
| 696 | 700 |
return real<Number>() * b; |
| 697 | 701 |
} |
| 698 | 702 |
|
| 699 | 703 |
/// \brief Returns a random real number from the range [a, b) |
| 700 | 704 |
/// |
| 701 | 705 |
/// It returns a random real number from the range [a, b). |
| 702 | 706 |
template <typename Number> |
| 703 | 707 |
Number real(Number a, Number b) {
|
| 704 | 708 |
return real<Number>() * (b - a) + a; |
| 705 | 709 |
} |
| 706 | 710 |
|
| 711 |
/// @} |
|
| 712 |
|
|
| 713 |
///\name Uniform distributions |
|
| 714 |
/// |
|
| 715 |
/// @{
|
|
| 716 |
|
|
| 707 | 717 |
/// \brief Returns a random real number from the range [0, 1) |
| 708 | 718 |
/// |
| 709 | 719 |
/// It returns a random double from the range [0, 1). |
| 710 | 720 |
double operator()() {
|
| 711 | 721 |
return real<double>(); |
| 712 | 722 |
} |
| 713 | 723 |
|
| 714 | 724 |
/// \brief Returns a random real number from the range [0, b) |
| 715 | 725 |
/// |
| 716 | 726 |
/// It returns a random real number from the range [0, b). |
| 717 | 727 |
template <typename Number> |
| 718 | 728 |
Number operator()(Number b) {
|
| ... | ... |
@@ -752,24 +762,26 @@ |
| 752 | 762 |
} |
| 753 | 763 |
|
| 754 | 764 |
/// \brief Returns a random non-negative integer |
| 755 | 765 |
/// |
| 756 | 766 |
/// It returns a random non-negative integer uniformly from the |
| 757 | 767 |
/// whole range of the current \c Number type. The default result |
| 758 | 768 |
/// type of this function is <tt>unsigned int</tt>. |
| 759 | 769 |
template <typename Number> |
| 760 | 770 |
Number uinteger() {
|
| 761 | 771 |
return _random_bits::IntConversion<Number, Word>::convert(core); |
| 762 | 772 |
} |
| 763 | 773 |
|
| 774 |
/// @} |
|
| 775 |
|
|
| 764 | 776 |
unsigned int uinteger() {
|
| 765 | 777 |
return uinteger<unsigned int>(); |
| 766 | 778 |
} |
| 767 | 779 |
|
| 768 | 780 |
/// \brief Returns a random integer |
| 769 | 781 |
/// |
| 770 | 782 |
/// It returns a random integer uniformly from the whole range of |
| 771 | 783 |
/// the current \c Number type. The default result type of this |
| 772 | 784 |
/// function is \c int. |
| 773 | 785 |
template <typename Number> |
| 774 | 786 |
Number integer() {
|
| 775 | 787 |
static const int nb = std::numeric_limits<Number>::digits + |
0 comments (0 inline)