00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <cmath>
00033 #include <iostream>
00034
00035 #include "Sacado_Random.hpp"
00036
00037 Sacado::Random::Random(double a_, double b_) :
00038 a(a_),
00039 b(b_),
00040 seed(static_cast<double>(rand()))
00041 {
00042
00043 if ((seed == 0.0) || (seed == 2147483647.0))
00044 seed = 1.0;
00045 }
00046
00047 Sacado::Random::Random(double a_, double b_, int s) :
00048 a(a_),
00049 b(b_),
00050 seed(0.0)
00051 {
00052 setSeed(s);
00053 }
00054
00055 Sacado::Random::~Random()
00056 {
00057 }
00058
00059 void
00060 Sacado::Random::setSeed(int s) {
00061 int ss = checkSeed("setSeed", s);
00062 srand(ss);
00063 seed = static_cast<double>(s);
00064 }
00065
00066 double
00067 Sacado::Random::number() {
00068 const double A = 16807.0;
00069 const double bigInt = 2147483647.0;
00070
00071 seed = std::fmod(A*seed, bigInt);
00072 return (b-a)*(seed/bigInt) + a;
00073 }
00074
00075 int
00076 Sacado::Random::checkSeed(const std::string& func, int s) {
00077 if ((s < 1) || (s > 2147483646)) {
00078 std::cerr << "Error in Sacado::Random::" << s << "(): "
00079 << "supplied seed "
00080 << s << " is not an integer between 1 and 2147483646."
00081 << std::endl << "Using a seed of 1 instead." << std::endl;
00082 return 1;
00083 }
00084 else
00085 return s;
00086 }