40 #include "CLHEP/Random/defs.h"
41 #include "CLHEP/Random/Random.h"
42 #include "CLHEP/Random/RanecuEngine.h"
43 #include "CLHEP/Random/engineIDulong.h"
50 static const int MarkerLen = 64;
52 static const double prec = 4.6566128E-10;
56 void RanecuEngine::further_randomize (
int seq1,
int col,
int index,
int modulus)
58 table[seq1][col] -= (index&0x3FFFFFFF);
59 while (table[seq1][col] <= 0) table[seq1][col] += (modulus-1);
63 int RanecuEngine::numEngines = 0;
68 int cycle = std::abs(
int(numEngines/maxSeq));
69 seq = std::abs(
int(numEngines%maxSeq));
72 long mask = ((cycle & 0x007fffff) << 8);
73 for (
int i=0; i<2; ++i) {
74 for (
int j=0; j<maxSeq; ++j) {
85 int cycle = std::abs(
int(index/maxSeq));
86 seq = std::abs(
int(index%maxSeq));
88 long mask = ((cycle & 0x000007ff) << 20);
89 for (
int j=0; j<maxSeq; ++j) {
95 further_randomize (seq, 0, index,
shift1);
108 seq = std::abs(
int(index%maxSeq));
112 further_randomize (seq, 0, index,
shift1);
113 further_randomize (seq, 1, dum,
shift2);
119 seq = std::abs(
int(pos%maxSeq));
123 table[seq][0] = std::abs(seeds[0])%
shift1;
124 table[seq][1] = std::abs(seeds[1])%
shift2;
130 seq = std::abs(
int(index%maxSeq));
137 std::ofstream outFile( filename, std::ios::out ) ;
139 if (!outFile.bad()) {
141 std::vector<unsigned long> v =
put();
143 std::cout <<
"Result of v = put() is:\n";
145 for (
unsigned int i=0; i<v.size(); ++i) {
146 outFile << v[i] <<
"\n";
148 std::cout << v[i] <<
" ";
149 if (i%6==0) std::cout <<
"\n";
157 if (!outFile.bad()) {
158 outFile <<
theSeed << std::endl;
159 for (
int i=0; i<2; ++i)
160 outFile << table[
theSeed][i] <<
" ";
161 outFile << std::endl;
168 std::ifstream inFile( filename, std::ios::in);
170 std::cerr <<
" -- Engine state remains unchanged\n";
174 std::vector<unsigned long> v;
179 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
180 if (ivec%3 == 0) std::cout <<
"\n";
183 inFile.clear(std::ios::badbit | inFile.rdstate());
184 std::cerr <<
"\nJamesRandom state (vector) description improper."
185 <<
"\nrestoreStatus has failed."
186 <<
"\nInput stream is probably mispositioned now." << std::endl;
195 if (!inFile.bad() && !inFile.eof()) {
197 for (
int i=0; i<2; ++i)
205 std::cout << std::endl;
206 std::cout <<
"--------- Ranecu engine status ---------" << std::endl;
207 std::cout <<
" Initial seed (index) = " <<
theSeed << std::endl;
208 std::cout <<
" Current couple of seeds = "
210 << table[
theSeed][1] << std::endl;
211 std::cout <<
"----------------------------------------" << std::endl;
216 const int index = seq;
217 long seed1 = table[index][0];
218 long seed2 = table[index][1];
224 if (seed1 < 0) seed1 +=
shift1;
226 if (seed2 < 0) seed2 +=
shift2;
228 table[index][0] = seed1;
229 table[index][1] = seed2;
231 long diff = seed1-seed2;
233 if (diff <= 0) diff += (
shift1-1);
234 return (
double)(diff*prec);
239 const int index = seq;
240 long seed1 = table[index][0];
241 long seed2 = table[index][1];
245 for (i=0; i<size; ++i)
251 if (seed1 < 0) seed1 +=
shift1;
253 if (seed2 < 0) seed2 +=
shift2;
255 long diff = seed1-seed2;
256 if (diff <= 0) diff += (
shift1-1);
258 vect[i] = (
double)(diff*prec);
260 table[index][0] = seed1;
261 table[index][1] = seed2;
264 RanecuEngine::operator
unsigned int() {
265 const int index = seq;
266 long seed1 = table[index][0];
267 long seed2 = table[index][1];
269 int k1 = (int)(seed1/ecuyer_b);
270 int k2 = (int)(seed2/ecuyer_e);
272 seed1 = ecuyer_a*(seed1-k1*ecuyer_b)-k1*ecuyer_c;
273 if (seed1 < 0) seed1 += shift1;
274 seed2 = ecuyer_d*(seed2-k2*ecuyer_e)-k2*ecuyer_f;
275 if (seed2 < 0) seed2 += shift2;
277 table[index][0] = seed1;
278 table[index][1] = seed2;
279 long diff = seed1-seed2;
280 if( diff <= 0 ) diff += (shift1-1);
282 return ((diff << 1) | (seed1&1))& 0xffffffff;
287 char beginMarker[] =
"RanecuEngine-begin";
288 os << beginMarker <<
"\nUvec\n";
289 std::vector<unsigned long> v =
put();
290 for (
unsigned int i=0; i<v.size(); ++i) {
295 char endMarker[] =
"RanecuEngine-end";
296 os <<
" " << beginMarker <<
"\n";
298 for (
int i=0; i<2; ++i) {
299 os << table[
theSeed][i] <<
"\n";
301 os << endMarker <<
"\n";
307 std::vector<unsigned long> v;
308 v.push_back (engineIDulong<RanecuEngine>());
309 v.push_back(static_cast<unsigned long>(
theSeed));
310 v.push_back(static_cast<unsigned long>(table[
theSeed][0]));
311 v.push_back(static_cast<unsigned long>(table[theSeed][1]));
317 char beginMarker [MarkerLen];
324 if (strcmp(beginMarker,
"RanecuEngine-begin")) {
325 is.clear(std::ios::badbit | is.rdstate());
326 std::cerr <<
"\nInput stream mispositioned or"
327 <<
"\nRanecuEngine state description missing or"
328 <<
"\nwrong engine type found." << std::endl;
335 return "RanecuEngine-begin";
341 std::vector<unsigned long> v;
346 is.clear(std::ios::badbit | is.rdstate());
347 std::cerr <<
"\nRanecuEngine state (vector) description improper."
348 <<
"\ngetState() has failed."
349 <<
"\nInput stream is probably mispositioned now." << std::endl;
359 char endMarker [MarkerLen];
360 for (
int i=0; i<2; ++i) {
366 if (strcmp(endMarker,
"RanecuEngine-end")) {
367 is.clear(std::ios::badbit | is.rdstate());
368 std::cerr <<
"\nRanecuEngine state description incomplete."
369 <<
"\nInput stream is probably mispositioned now." << std::endl;
378 if ((v[0] & 0xffffffffUL) != engineIDulong<RanecuEngine>()) {
380 "\nRanecuEngine get:state vector has wrong ID word - state unchanged\n";
389 "\nRanecuEngine get:state vector has wrong length - state unchanged\n";
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
static const int ecuyer_e
void restoreStatus(const char filename[]="Ranecu.conf")
static std::string engineName()
static const int ecuyer_f
static const int ecuyer_b
static const int ecuyer_c
void setSeeds(const long *seeds, int index=-1)
void setSeed(long index, int dum=0)
static const int ecuyer_a
virtual std::istream & getState(std::istream &is)
void setIndex(long index)
static const unsigned int VECTOR_STATE_SIZE
virtual std::istream & get(std::istream &is)
void flatArray(const int size, double *vect)
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
static void getTheTableSeeds(long *seeds, int index)
std::vector< unsigned long > put() const
void saveStatus(const char filename[]="Ranecu.conf") const
static const int ecuyer_d
static std::string beginTag()