49 static const int MarkerLen = 64;
51 static const double prec = 4.6566128E-10;
55 void RanecuEngine::further_randomize (
int seq1,
int col,
int index,
int modulus)
57 table[seq1][
col] -= (index&0x3FFFFFFF);
58 while (table[seq1][col] <= 0) table[seq1][
col] += (modulus-1);
62 int RanecuEngine::numEngines = 0;
67 int cycle = std::abs(
int(numEngines/maxSeq));
68 seq = std::abs(
int(numEngines%maxSeq));
71 long mask = ((cycle & 0x007fffff) << 8);
72 for (
int i=0; i<2; ++i) {
73 for (
int j=0; j<maxSeq; ++j) {
84 int cycle = std::abs(
int(index/maxSeq));
85 seq = std::abs(
int(index%maxSeq));
87 long mask = ((cycle & 0x000007ff) << 20);
88 for (
int j=0; j<maxSeq; ++j) {
94 further_randomize (seq, 0, index,
shift1);
107 seq = std::abs(
int(index%maxSeq));
111 further_randomize (seq, 0, index,
shift1);
112 further_randomize (seq, 1, dum,
shift2);
118 seq = std::abs(
int(pos%maxSeq));
122 table[seq][0] = std::abs(seeds[0])%
shift1;
123 table[seq][1] = std::abs(seeds[1])%
shift2;
129 seq = std::abs(
int(index%maxSeq));
136 std::ofstream
outFile( filename, std::ios::out ) ;
138 if (!outFile.bad()) {
140 std::vector<unsigned long>
v =
put();
141 for (
unsigned int i=0; i<v.size(); ++i) {
142 outFile << v[i] <<
"\n";
151 std::cerr <<
" -- Engine state remains unchanged\n";
155 std::vector<unsigned long>
v;
160 inFile.clear(std::ios::badbit | inFile.rdstate());
161 std::cerr <<
"\nJamesRandom state (vector) description improper."
162 <<
"\nrestoreStatus has failed."
163 <<
"\nInput stream is probably mispositioned now." << std::endl;
172 if (!inFile.bad() && !inFile.eof()) {
174 for (
int i=0; i<2; ++i)
182 std::cout << std::endl;
183 std::cout <<
"--------- Ranecu engine status ---------" << std::endl;
184 std::cout <<
" Initial seed (index) = " <<
theSeed << std::endl;
185 std::cout <<
" Current couple of seeds = "
187 << table[
theSeed][1] << std::endl;
188 std::cout <<
"----------------------------------------" << std::endl;
193 const int index = seq;
194 long seed1 = table[
index][0];
195 long seed2 = table[
index][1];
201 if (seed1 < 0) seed1 +=
shift1;
203 if (seed2 < 0) seed2 +=
shift2;
205 table[
index][0] = seed1;
206 table[
index][1] = seed2;
208 long diff = seed1-seed2;
210 if (diff <= 0) diff += (
shift1-1);
211 return (
double)(diff*prec);
216 const int index = seq;
217 long seed1 = table[
index][0];
218 long seed2 = table[
index][1];
222 for (i=0; i<size; ++i)
228 if (seed1 < 0) seed1 +=
shift1;
230 if (seed2 < 0) seed2 +=
shift2;
232 long diff = seed1-seed2;
233 if (diff <= 0) diff += (
shift1-1);
235 vect[i] = (double)(diff*prec);
237 table[
index][0] = seed1;
238 table[
index][1] = seed2;
241 RanecuEngine::operator
unsigned int() {
242 const int index = seq;
243 long seed1 = table[
index][0];
244 long seed2 = table[
index][1];
246 int k1 = (
int)(seed1/ecuyer_b);
247 int k2 = (
int)(seed2/ecuyer_e);
249 seed1 = ecuyer_a*(seed1-k1*ecuyer_b)-k1*ecuyer_c;
250 if (seed1 < 0) seed1 += shift1;
251 seed2 = ecuyer_d*(seed2-k2*ecuyer_e)-k2*ecuyer_f;
252 if (seed2 < 0) seed2 += shift2;
254 table[
index][0] = seed1;
255 table[
index][1] = seed2;
256 long diff = seed1-seed2;
257 if( diff <= 0 ) diff += (shift1-1);
259 return ((diff << 1) | (seed1&1))& 0xffffffff;
264 char beginMarker[] =
"RanecuEngine-begin";
265 os << beginMarker <<
"\nUvec\n";
266 std::vector<unsigned long>
v =
put();
267 for (
unsigned int i=0; i<v.size(); ++i) {
274 std::vector<unsigned long>
v;
275 v.push_back (engineIDulong<RanecuEngine>());
276 v.push_back(static_cast<unsigned long>(
theSeed));
277 v.push_back(static_cast<unsigned long>(table[
theSeed][0]));
278 v.push_back(static_cast<unsigned long>(table[theSeed][1]));
284 char beginMarker [MarkerLen];
291 if (strcmp(beginMarker,
"RanecuEngine-begin")) {
292 is.clear(std::ios::badbit | is.rdstate());
293 std::cerr <<
"\nInput stream mispositioned or"
294 <<
"\nRanecuEngine state description missing or"
295 <<
"\nwrong engine type found." << std::endl;
302 return "RanecuEngine-begin";
308 std::vector<unsigned long>
v;
313 is.clear(std::ios::badbit | is.rdstate());
314 std::cerr <<
"\nRanecuEngine state (vector) description improper."
315 <<
"\ngetState() has failed."
316 <<
"\nInput stream is probably mispositioned now." << std::endl;
326 char endMarker [MarkerLen];
327 for (
int i=0; i<2; ++i) {
333 if (strcmp(endMarker,
"RanecuEngine-end")) {
334 is.clear(std::ios::badbit | is.rdstate());
335 std::cerr <<
"\nRanecuEngine state description incomplete."
336 <<
"\nInput stream is probably mispositioned now." << std::endl;
345 if ((v[0] & 0xffffffffUL) != engineIDulong<RanecuEngine>()) {
347 "\nRanecuEngine get:state vector has wrong ID word - state unchanged\n";
356 "\nRanecuEngine get:state vector has wrong length - state unchanged\n";
void setSeeds(const long *seeds, int index=-1)
static const int ecuyer_b
static const int ecuyer_f
static std::string engineName()
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
static const unsigned int VECTOR_STATE_SIZE
void restoreStatus(const char filename[]="Ranecu.conf")
virtual std::istream & getState(std::istream &is)
void setIndex(long index)
virtual std::istream & get(std::istream &is)
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
static const int ecuyer_a
void flatArray(const int size, double *vect)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
static const int ecuyer_e
void saveStatus(const char filename[]="Ranecu.conf") const
static const int ecuyer_d
static std::string beginTag()
void setSeed(long index, int dum=0)
static const int ecuyer_c
std::vector< unsigned long > put() const
static void getTheTableSeeds(long *seeds, int index)