40#include "CLHEP/Random/defs.h"
41#include "CLHEP/Random/Random.h"
42#include "CLHEP/Random/RanecuEngine.h"
43#include "CLHEP/Random/engineIDulong.h"
44#include "CLHEP/Utility/atomic_int.h"
62static const int MarkerLen = 64;
64static const double prec = 4.6566128E-10;
68void RanecuEngine::further_randomize (
int seq1,
int col,
int index,
int modulus)
70 table[seq1][col] -= (index&0x3FFFFFFF);
71 while (table[seq1][col] <= 0) table[seq1][col] += (modulus-1);
77 int numEngines = numberOfEngines++;
78 int cycle = std::abs(
int(numEngines/maxSeq));
79 seq = std::abs(
int(numEngines%maxSeq));
82 long mask = ((cycle & 0x007fffff) << 8);
83 for (
int i=0; i<2; ++i) {
84 for (
int j=0; j<maxSeq; ++j) {
95 int cycle = std::abs(
int(index/maxSeq));
96 seq = std::abs(
int(index%maxSeq));
98 long mask = ((cycle & 0x000007ff) << 20);
99 for (
int j=0; j<maxSeq; ++j) {
105 further_randomize (seq, 0, index,
shift1);
118 seq = std::abs(
int(index%maxSeq));
122 further_randomize (seq, 0, (
int)index,
shift1);
123 further_randomize (seq, 1, dum,
shift2);
129 seq = std::abs(
int(pos%maxSeq));
133 table[seq][0] = std::abs(seeds[0])%
shift1;
134 table[seq][1] = std::abs(seeds[1])%
shift2;
140 seq = std::abs(
int(index%maxSeq));
147 std::ofstream outFile( filename, std::ios::out ) ;
149 if (!outFile.bad()) {
151 std::vector<unsigned long> v =
put();
153 std::cout <<
"Result of v = put() is:\n";
155 for (
unsigned int i=0; i<v.size(); ++i) {
156 outFile << v[i] <<
"\n";
158 std::cout << v[i] <<
" ";
159 if (i%6==0) std::cout <<
"\n";
167 if (!outFile.bad()) {
168 outFile <<
theSeed << std::endl;
169 for (
int i=0; i<2; ++i)
170 outFile << table[
theSeed][i] <<
" ";
171 outFile << std::endl;
178 std::ifstream inFile( filename, std::ios::in);
180 std::cerr <<
" -- Engine state remains unchanged\n";
184 std::vector<unsigned long> v;
189 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
190 if (ivec%3 == 0) std::cout <<
"\n";
193 inFile.clear(std::ios::badbit | inFile.rdstate());
194 std::cerr <<
"\nJamesRandom state (vector) description improper."
195 <<
"\nrestoreStatus has failed."
196 <<
"\nInput stream is probably mispositioned now." << std::endl;
205 if (!inFile.bad() && !inFile.eof()) {
207 for (
int i=0; i<2; ++i)
215 std::cout << std::endl;
216 std::cout <<
"--------- Ranecu engine status ---------" << std::endl;
217 std::cout <<
" Initial seed (index) = " <<
theSeed << std::endl;
218 std::cout <<
" Current couple of seeds = "
220 << table[
theSeed][1] << std::endl;
221 std::cout <<
"----------------------------------------" << std::endl;
226 const int index = seq;
227 long seed1 = table[index][0];
228 long seed2 = table[index][1];
234 if (seed1 < 0) seed1 +=
shift1;
236 if (seed2 < 0) seed2 +=
shift2;
238 table[index][0] = seed1;
239 table[index][1] = seed2;
241 long diff = seed1-seed2;
243 if (diff <= 0) diff += (
shift1-1);
244 return (
double)(diff*prec);
249 const int index = seq;
250 long seed1 = table[index][0];
251 long seed2 = table[index][1];
255 for (i=0; i<size; ++i)
261 if (seed1 < 0) seed1 +=
shift1;
263 if (seed2 < 0) seed2 +=
shift2;
265 long diff = seed1-seed2;
266 if (diff <= 0) diff += (
shift1-1);
268 vect[i] = (
double)(diff*prec);
270 table[index][0] = seed1;
271 table[index][1] = seed2;
278RanecuEngine::operator float() {
279 return float( flat() );
282RanecuEngine::operator
unsigned int() {
283 const int index = seq;
284 long seed1 = table[index][0];
285 long seed2 = table[index][1];
287 int k1 = (int)(seed1/ecuyer_b);
288 int k2 = (int)(seed2/ecuyer_e);
290 seed1 = ecuyer_a*(seed1-k1*ecuyer_b)-k1*ecuyer_c;
291 if (seed1 < 0) seed1 += shift1;
292 seed2 = ecuyer_d*(seed2-k2*ecuyer_e)-k2*ecuyer_f;
293 if (seed2 < 0) seed2 += shift2;
295 table[index][0] = seed1;
296 table[index][1] = seed2;
297 long diff = seed1-seed2;
298 if( diff <= 0 ) diff += (shift1-1);
300 return ((diff << 1) | (seed1&1))& 0xffffffff;
305 char beginMarker[] =
"RanecuEngine-begin";
306 os << beginMarker <<
"\nUvec\n";
307 std::vector<unsigned long> v =
put();
308 for (
unsigned int i=0; i<v.size(); ++i) {
313 char endMarker[] =
"RanecuEngine-end";
314 os <<
" " << beginMarker <<
"\n";
316 for (
int i=0; i<2; ++i) {
317 os << table[
theSeed][i] <<
"\n";
319 os << endMarker <<
"\n";
325 std::vector<unsigned long> v;
326 v.push_back (engineIDulong<RanecuEngine>());
327 v.push_back(
static_cast<unsigned long>(
theSeed));
328 v.push_back(
static_cast<unsigned long>(table[
theSeed][0]));
329 v.push_back(
static_cast<unsigned long>(table[
theSeed][1]));
335 char beginMarker [MarkerLen];
342 if (strcmp(beginMarker,
"RanecuEngine-begin")) {
343 is.clear(std::ios::badbit | is.rdstate());
344 std::cerr <<
"\nInput stream mispositioned or"
345 <<
"\nRanecuEngine state description missing or"
346 <<
"\nwrong engine type found." << std::endl;
353 return "RanecuEngine-begin";
359 std::vector<unsigned long> v;
364 is.clear(std::ios::badbit | is.rdstate());
365 std::cerr <<
"\nRanecuEngine state (vector) description improper."
366 <<
"\ngetState() has failed."
367 <<
"\nInput stream is probably mispositioned now." << std::endl;
377 char endMarker [MarkerLen];
378 for (
int i=0; i<2; ++i) {
384 if (strcmp(endMarker,
"RanecuEngine-end")) {
385 is.clear(std::ios::badbit | is.rdstate());
386 std::cerr <<
"\nRanecuEngine state description incomplete."
387 <<
"\nInput stream is probably mispositioned now." << std::endl;
396 if ((v[0] & 0xffffffffUL) != engineIDulong<RanecuEngine>()) {
398 "\nRanecuEngine get:state vector has wrong ID word - state unchanged\n";
407 "\nRanecuEngine get:state vector has wrong length - state unchanged\n";
#define CLHEP_ATOMIC_INT_TYPE
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)
void restoreStatus(const char filename[]="Ranecu.conf")
void saveStatus(const char filename[]="Ranecu.conf") const
static const int ecuyer_b
static const int ecuyer_e
static std::string engineName()
static const int ecuyer_f
std::vector< unsigned long > put() const
void setSeed(long index, int dum=0)
static const int ecuyer_d
virtual std::istream & get(std::istream &is)
void flatArray(const int size, double *vect)
virtual std::istream & getState(std::istream &is)
void setIndex(long index)
static const int ecuyer_a
static const unsigned int VECTOR_STATE_SIZE
static const int ecuyer_c
void setSeeds(const long *seeds, int index=-1)
static std::string beginTag()
bool possibleKeywordInput(IS &is, const std::string &key, T &t)