... | ... |
@@ -647,139 +647,121 @@ |
647 | 647 |
{ |
648 | 648 |
std::ifstream rs(file.c_str()); |
649 | 649 |
const int size = 4; |
650 | 650 |
Word buf[size]; |
651 | 651 |
if (offset != 0 && !rs.seekg(offset)) return false; |
652 | 652 |
if (!rs.read(reinterpret_cast<char*>(buf), sizeof(buf))) return false; |
653 | 653 |
seed(buf, buf + size); |
654 | 654 |
return true; |
655 | 655 |
} |
656 | 656 |
|
657 | 657 |
/// \brief Seding from process id and time |
658 | 658 |
/// |
659 | 659 |
/// Seding from process id and time. This function uses the |
660 | 660 |
/// current process id and the current time for initialize the |
661 | 661 |
/// random sequence. |
662 | 662 |
/// \return Currently always true. |
663 | 663 |
bool seedFromTime() { |
664 | 664 |
#ifndef WIN32 |
665 | 665 |
timeval tv; |
666 | 666 |
gettimeofday(&tv, 0); |
667 | 667 |
seed(getpid() + tv.tv_sec + tv.tv_usec); |
668 | 668 |
#else |
669 | 669 |
FILETIME time; |
670 | 670 |
GetSystemTimeAsFileTime(&time); |
671 | 671 |
seed(GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime); |
672 | 672 |
#endif |
673 | 673 |
return true; |
674 | 674 |
} |
675 | 675 |
|
676 | 676 |
/// @} |
677 | 677 |
|
678 | 678 |
///\name Uniform distributions |
679 | 679 |
/// |
680 | 680 |
/// @{ |
681 | 681 |
|
682 | 682 |
/// \brief Returns a random real number from the range [0, 1) |
683 | 683 |
/// |
684 | 684 |
/// It returns a random real number from the range [0, 1). The |
685 | 685 |
/// default Number type is \c double. |
686 | 686 |
template <typename Number> |
687 | 687 |
Number real() { |
688 | 688 |
return _random_bits::RealConversion<Number, Word>::convert(core); |
689 | 689 |
} |
690 | 690 |
|
691 | 691 |
double real() { |
692 | 692 |
return real<double>(); |
693 | 693 |
} |
694 | 694 |
|
695 |
/// \brief Returns a random real number the range [0, b) |
|
696 |
/// |
|
697 |
/// It returns a random real number from the range [0, b). |
|
698 |
template <typename Number> |
|
699 |
Number real(Number b) { |
|
700 |
return real<Number>() * b; |
|
701 |
} |
|
702 |
|
|
703 |
/// \brief Returns a random real number from the range [a, b) |
|
704 |
/// |
|
705 |
/// It returns a random real number from the range [a, b). |
|
706 |
template <typename Number> |
|
707 |
Number real(Number a, Number b) { |
|
708 |
return real<Number>() * (b - a) + a; |
|
709 |
} |
|
710 |
|
|
711 | 695 |
/// @} |
712 | 696 |
|
713 | 697 |
///\name Uniform distributions |
714 | 698 |
/// |
715 | 699 |
/// @{ |
716 | 700 |
|
717 | 701 |
/// \brief Returns a random real number from the range [0, 1) |
718 | 702 |
/// |
719 | 703 |
/// It returns a random double from the range [0, 1). |
720 | 704 |
double operator()() { |
721 | 705 |
return real<double>(); |
722 | 706 |
} |
723 | 707 |
|
724 | 708 |
/// \brief Returns a random real number from the range [0, b) |
725 | 709 |
/// |
726 | 710 |
/// It returns a random real number from the range [0, b). |
727 |
template <typename Number> |
|
728 |
Number operator()(Number b) { |
|
729 |
|
|
711 |
double operator()(double b) { |
|
712 |
return real<double>() * b; |
|
730 | 713 |
} |
731 | 714 |
|
732 | 715 |
/// \brief Returns a random real number from the range [a, b) |
733 | 716 |
/// |
734 | 717 |
/// It returns a random real number from the range [a, b). |
735 |
template <typename Number> |
|
736 |
Number operator()(Number a, Number b) { |
|
737 |
|
|
718 |
double operator()(double a, double b) { |
|
719 |
return real<double>() * (b - a) + a; |
|
738 | 720 |
} |
739 | 721 |
|
740 | 722 |
/// \brief Returns a random integer from a range |
741 | 723 |
/// |
742 | 724 |
/// It returns a random integer from the range {0, 1, ..., b - 1}. |
743 | 725 |
template <typename Number> |
744 | 726 |
Number integer(Number b) { |
745 | 727 |
return _random_bits::Mapping<Number, Word>::map(core, b); |
746 | 728 |
} |
747 | 729 |
|
748 | 730 |
/// \brief Returns a random integer from a range |
749 | 731 |
/// |
750 | 732 |
/// It returns a random integer from the range {a, a + 1, ..., b - 1}. |
751 | 733 |
template <typename Number> |
752 | 734 |
Number integer(Number a, Number b) { |
753 | 735 |
return _random_bits::Mapping<Number, Word>::map(core, b - a) + a; |
754 | 736 |
} |
755 | 737 |
|
756 | 738 |
/// \brief Returns a random integer from a range |
757 | 739 |
/// |
758 | 740 |
/// It returns a random integer from the range {0, 1, ..., b - 1}. |
759 | 741 |
template <typename Number> |
760 | 742 |
Number operator[](Number b) { |
761 | 743 |
return _random_bits::Mapping<Number, Word>::map(core, b); |
762 | 744 |
} |
763 | 745 |
|
764 | 746 |
/// \brief Returns a random non-negative integer |
765 | 747 |
/// |
766 | 748 |
/// It returns a random non-negative integer uniformly from the |
767 | 749 |
/// whole range of the current \c Number type. The default result |
768 | 750 |
/// type of this function is <tt>unsigned int</tt>. |
769 | 751 |
template <typename Number> |
770 | 752 |
Number uinteger() { |
771 | 753 |
return _random_bits::IntConversion<Number, Word>::convert(core); |
772 | 754 |
} |
773 | 755 |
|
774 | 756 |
/// @} |
775 | 757 |
|
776 | 758 |
unsigned int uinteger() { |
777 | 759 |
return uinteger<unsigned int>(); |
778 | 760 |
} |
779 | 761 |
|
780 | 762 |
/// \brief Returns a random integer |
781 | 763 |
/// |
782 | 764 |
/// It returns a random integer uniformly from the whole range of |
783 | 765 |
/// the current \c Number type. The default result type of this |
784 | 766 |
/// function is \c int. |
785 | 767 |
template <typename Number> |
0 comments (0 inline)