58 static const double prec = 4.6566128E-10;
62 void RanecuEngine::further_randomize (
int seq1,
int col,
int index,
int modulus)
64 table[seq1][col] -= (index&0x3FFFFFFF);
65 while (table[seq1][col] <= 0) table[seq1][col] += (modulus-1);
71 int numEngines = numberOfEngines++;
72 int cycle = std::abs(
int(numEngines/maxSeq));
73 seq = std::abs(
int(numEngines%maxSeq));
76 long mask = ((cycle & 0x007fffff) << 8);
77 for (
int i=0; i<2; ++i) {
78 for (
int j=0; j<maxSeq; ++j) {
89 int cycle = std::abs(
int(index/maxSeq));
90 seq = std::abs(
int(index%maxSeq));
92 long mask = ((cycle & 0x000007ff) << 20);
93 for (
int j=0; j<maxSeq; ++j) {
99 further_randomize (seq, 0, index,
shift1);
112 seq = std::abs(
int(index%maxSeq));
116 further_randomize (seq, 0, index,
shift1);
117 further_randomize (seq, 1, dum,
shift2);
123 seq = std::abs(
int(pos%maxSeq));
127 table[seq][0] = std::abs(seeds[0])%
shift1;
128 table[seq][1] = std::abs(seeds[1])%
shift2;
134 seq = std::abs(
int(index%maxSeq));
141 std::ofstream outFile( filename, std::ios::out ) ;
143 if (!outFile.bad()) {
145 std::vector<unsigned long>
v =
put();
146 for (
unsigned int i=0; i<v.size(); ++i) {
147 outFile << v[i] <<
"\n";
154 std::ifstream inFile( filename, std::ios::in);
156 std::cerr <<
" -- Engine state remains unchanged\n";
160 std::vector<unsigned long>
v;
165 inFile.clear(std::ios::badbit | inFile.rdstate());
166 std::cerr <<
"\nJamesRandom state (vector) description improper."
167 <<
"\nrestoreStatus has failed."
168 <<
"\nInput stream is probably mispositioned now." << std::endl;
177 if (!inFile.bad() && !inFile.eof()) {
179 for (
int i=0; i<2; ++i)
187 std::cout << std::endl;
188 std::cout <<
"--------- Ranecu engine status ---------" << std::endl;
189 std::cout <<
" Initial seed (index) = " <<
theSeed << std::endl;
190 std::cout <<
" Current couple of seeds = "
192 << table[
theSeed][1] << std::endl;
193 std::cout <<
"----------------------------------------" << std::endl;
198 const int index = seq;
199 long seed1 = table[index][0];
200 long seed2 = table[index][1];
206 if (seed1 < 0) seed1 +=
shift1;
208 if (seed2 < 0) seed2 +=
shift2;
210 table[index][0] = seed1;
211 table[index][1] = seed2;
213 long diff = seed1-seed2;
215 if (diff <= 0) diff += (
shift1-1);
216 return (
double)(diff*
prec);
221 const int index = seq;
222 long seed1 = table[index][0];
223 long seed2 = table[index][1];
227 for (i=0; i<size; ++i)
233 if (seed1 < 0) seed1 +=
shift1;
235 if (seed2 < 0) seed2 +=
shift2;
237 long diff = seed1-seed2;
238 if (diff <= 0) diff += (
shift1-1);
240 vect[i] = (double)(diff*
prec);
242 table[index][0] = seed1;
243 table[index][1] = seed2;
246 RanecuEngine::operator
unsigned int() {
247 const int index = seq;
248 long seed1 = table[index][0];
249 long seed2 = table[index][1];
251 int k1 = (
int)(seed1/ecuyer_b);
252 int k2 = (
int)(seed2/ecuyer_e);
254 seed1 = ecuyer_a*(seed1-k1*ecuyer_b)-k1*ecuyer_c;
255 if (seed1 < 0) seed1 += shift1;
256 seed2 = ecuyer_d*(seed2-k2*ecuyer_e)-k2*ecuyer_f;
257 if (seed2 < 0) seed2 += shift2;
259 table[index][0] = seed1;
260 table[index][1] = seed2;
261 long diff = seed1-seed2;
262 if( diff <= 0 ) diff += (shift1-1);
264 return ((diff << 1) | (seed1&1))& 0xffffffff;
269 char beginMarker[] =
"RanecuEngine-begin";
270 os << beginMarker <<
"\nUvec\n";
271 std::vector<unsigned long>
v =
put();
272 for (
unsigned int i=0; i<v.size(); ++i) {
279 std::vector<unsigned long>
v;
280 v.push_back (engineIDulong<RanecuEngine>());
281 v.push_back(static_cast<unsigned long>(
theSeed));
282 v.push_back(static_cast<unsigned long>(table[
theSeed][0]));
283 v.push_back(static_cast<unsigned long>(table[theSeed][1]));
296 if (strcmp(beginMarker,
"RanecuEngine-begin")) {
297 is.clear(std::ios::badbit | is.rdstate());
298 std::cerr <<
"\nInput stream mispositioned or"
299 <<
"\nRanecuEngine state description missing or"
300 <<
"\nwrong engine type found." << std::endl;
307 return "RanecuEngine-begin";
313 std::vector<unsigned long>
v;
318 is.clear(std::ios::badbit | is.rdstate());
319 std::cerr <<
"\nRanecuEngine state (vector) description improper."
320 <<
"\ngetState() has failed."
321 <<
"\nInput stream is probably mispositioned now." << std::endl;
332 for (
int i=0; i<2; ++i) {
338 if (strcmp(endMarker,
"RanecuEngine-end")) {
339 is.clear(std::ios::badbit | is.rdstate());
340 std::cerr <<
"\nRanecuEngine state description incomplete."
341 <<
"\nInput stream is probably mispositioned now." << std::endl;
350 if ((v[0] & 0xffffffffUL) != engineIDulong<RanecuEngine>()) {
352 "\nRanecuEngine get:state vector has wrong ID word - state unchanged\n";
361 "\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 MarkerLen
static const int ecuyer_f
static std::string engineName()
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)
#define CLHEP_ATOMIC_INT_TYPE
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
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
static const G4double pos
std::vector< unsigned long > put() const
static void getTheTableSeeds(long *seeds, int index)