70static const int MarkerLen = 64;
82 numEngines(numberOfEngines++),
83 tausworthe (1234567 + numEngines + 175321),
84 integerCong(69607 * tausworthe + 54329, numEngines)
92 tausworthe ((unsigned int)seed + 175321),
93 integerCong(69607 * tausworthe + 54329, 8043)
108 tausworthe (rowIndex + 1000 * colIndex + 85329),
109 integerCong(69607 * tausworthe + 54329, 1123)
117 unsigned int ic ( integerCong );
118 unsigned int t ( tausworthe );
126 for (
int i = 0; i < size; ++i) {
133 tausworthe = Tausworthe((
unsigned int)seed + 175321);
134 integerCong = IntegerCong(69607 * tausworthe + 54329, 8043);
138 setSeed(seeds ? *seeds : 1234567, 0);
143 std::ofstream outFile(filename, std::ios::out);
144 if (!outFile.bad()) {
146 std::vector<unsigned long> v =
put();
147 for (
unsigned int i=0; i<v.size(); ++i) {
148 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 <<
"\nDualRand state (vector) description improper."
167 <<
"\nrestoreStatus has failed."
168 <<
"\nInput stream is probably mispositioned now." << std::endl;
179 tausworthe.get(inFile);
180 integerCong.get(inFile);
185 long pr=std::cout.precision(20);
186 std::cout << std::endl;
187 std::cout <<
"-------- DualRand engine status ---------"
189 std::cout <<
"Initial seed = " <<
theSeed << std::endl;
190 std::cout <<
"Tausworthe generator = " << std::endl;
191 tausworthe.put(std::cout);
192 std::cout <<
"\nIntegerCong generator = " << std::endl;
193 integerCong.put(std::cout);
194 std::cout << std::endl <<
"-----------------------------------------"
196 std::cout.precision(pr);
199DualRand::operator double() {
203DualRand::operator float() {
204 return (
float) ( (integerCong ^ tausworthe) * twoToMinus_32()
205 + nearlyTwoToMinus_54() );
209DualRand::operator
unsigned int() {
210 return (integerCong ^ tausworthe) & 0xffffffff;
214 char beginMarker[] =
"DualRand-begin";
215 os << beginMarker <<
"\nUvec\n";
216 std::vector<unsigned long> v =
put();
217 for (
unsigned int i=0; i<v.size(); ++i) {
224 std::vector<unsigned long> v;
232 char beginMarker [MarkerLen];
238 if (strcmp(beginMarker,
"DualRand-begin")) {
239 is.clear(std::ios::badbit | is.rdstate());
240 std::cerr <<
"\nInput mispositioned or"
241 <<
"\nDualRand state description missing or"
242 <<
"\nwrong engine type found." << std::endl;
249 return "DualRand-begin";
254 std::vector<unsigned long> v;
259 is.clear(std::ios::badbit | is.rdstate());
260 std::cerr <<
"\nDualRand state (vector) description improper."
261 <<
"\ngetState() has failed."
262 <<
"\nInput stream is probably mispositioned now." << std::endl;
273 char endMarker [MarkerLen];
279 if (strcmp(endMarker,
"DualRand-end")) {
280 is.clear(std::ios::badbit | is.rdstate());
281 std::cerr <<
"DualRand state description incomplete."
282 <<
"\nInput stream is probably mispositioned now." << std::endl;
291 "\nDualRand get:state vector has wrong ID word - state unchanged\n";
295 std::cerr <<
"\nDualRand get:state vector has wrong size: "
296 << v.size() <<
" - state unchanged\n";
303 std::vector<unsigned long>::const_iterator iv = v.begin()+1;
304 if (!tausworthe.get(iv))
return false;
305 if (!integerCong.get(iv))
return false;
308 "\nDualRand get:state vector has wrong size: " << v.size()
309 <<
"\n Apparently " << iv-v.begin() <<
" words were consumed\n";
315DualRand::Tausworthe::Tausworthe() {
317 for (wordIndex = 1; wordIndex < 4; ++wordIndex) {
318 words[wordIndex] = 69607 * words[wordIndex-1] + 54329;
322DualRand::Tausworthe::Tausworthe(
unsigned int seed) {
324 for (wordIndex = 1; wordIndex < 4; ++wordIndex) {
325 words[wordIndex] = 69607 * words[wordIndex-1] + 54329;
329DualRand::Tausworthe::operator
unsigned int() {
375 if (wordIndex <= 0) {
376 for (wordIndex = 0; wordIndex < 4; ++wordIndex) {
377 words[wordIndex] = ( (words[(wordIndex+1) & 3] << 1 ) |
378 (words[wordIndex] >> 31) )
379 ^ ( (words[(wordIndex+1) & 3] << 31) |
380 (words[wordIndex] >> 1) );
383 return words[--wordIndex] & 0xffffffff;
386void DualRand::Tausworthe::put(std::ostream & os)
const {
387 char beginMarker[] =
"Tausworthe-begin";
388 char endMarker[] =
"Tausworthe-end";
390 long pr=os.precision(20);
391 os <<
" " << beginMarker <<
" ";
392 for (
int i = 0; i < 4; ++i) {
393 os << words[i] <<
" ";
396 os <<
" " << endMarker <<
" ";
401void DualRand::Tausworthe::put(std::vector<unsigned long> & v)
const {
402 for (
int i = 0; i < 4; ++i) {
403 v.push_back(
static_cast<unsigned long>(words[i]));
405 v.push_back(
static_cast<unsigned long>(wordIndex));
408void DualRand::Tausworthe::get(std::istream & is) {
409 char beginMarker [MarkerLen];
410 char endMarker [MarkerLen];
417 if (strcmp(beginMarker,
"Tausworthe-begin")) {
418 is.clear(std::ios::badbit | is.rdstate());
419 std::cerr <<
"\nInput mispositioned or"
420 <<
"\nTausworthe state description missing or"
421 <<
"\nwrong engine type found." << std::endl;
423 for (
int i = 0; i < 4; ++i) {
430 if (strcmp(endMarker,
"Tausworthe-end")) {
431 is.clear(std::ios::badbit | is.rdstate());
432 std::cerr <<
"\nTausworthe state description incomplete."
433 <<
"\nInput stream is probably mispositioned now." << std::endl;
438DualRand::Tausworthe::get(std::vector<unsigned long>::const_iterator & iv){
439 for (
int i = 0; i < 4; ++i) {
440 words[i] = (
unsigned int)*iv++;
442 wordIndex = (int)*iv++;
446DualRand::IntegerCong::IntegerCong()
447: state((unsigned int)3758656018U),
453DualRand::IntegerCong::IntegerCong(
unsigned int seed,
int streamNumber)
455 multiplier(65536 + 1024 + 5 + (8 * 1017 * streamNumber)),
473DualRand::IntegerCong::operator
unsigned int() {
474 return state = (state * multiplier + addend) & 0xffffffff;
477void DualRand::IntegerCong::put(std::ostream & os)
const {
478 char beginMarker[] =
"IntegerCong-begin";
479 char endMarker[] =
"IntegerCong-end";
481 long pr=os.precision(20);
482 os <<
" " << beginMarker <<
" ";
483 os << state <<
" " << multiplier <<
" " << addend;
484 os <<
" " << endMarker <<
" ";
489void DualRand::IntegerCong::put(std::vector<unsigned long> & v)
const {
490 v.push_back(
static_cast<unsigned long>(state));
491 v.push_back(
static_cast<unsigned long>(multiplier));
492 v.push_back(
static_cast<unsigned long>(addend));
495void DualRand::IntegerCong::get(std::istream & is) {
496 char beginMarker [MarkerLen];
497 char endMarker [MarkerLen];
504 if (strcmp(beginMarker,
"IntegerCong-begin")) {
505 is.clear(std::ios::badbit | is.rdstate());
506 std::cerr <<
"\nInput mispositioned or"
507 <<
"\nIntegerCong state description missing or"
508 <<
"\nwrong engine type found." << std::endl;
510 is >> state >> multiplier >> addend;
514 if (strcmp(endMarker,
"IntegerCong-end")) {
515 is.clear(std::ios::badbit | is.rdstate());
516 std::cerr <<
"\nIntegerCong state description incomplete."
517 <<
"\nInput stream is probably mispositioned now." << std::endl;
522DualRand::IntegerCong::get(std::vector<unsigned long>::const_iterator & iv) {
523 state = (
unsigned int)*iv++;
524 multiplier = (
unsigned int)*iv++;
525 addend = (
unsigned int)*iv++;
#define CLHEP_ATOMIC_INT_TYPE
void setSeeds(const long *seeds, int)
std::vector< unsigned long > put() const
static const unsigned int VECTOR_STATE_SIZE
void restoreStatus(const char filename[]="DualRand.conf")
void setSeed(long seed, int)
void saveStatus(const char filename[]="DualRand.conf") const
void flatArray(const int size, double *vect)
virtual std::istream & get(std::istream &is)
static std::string engineName()
virtual std::istream & getState(std::istream &is)
static std::string beginTag()
static double twoToMinus_32()
static double twoToMinus_53()
static double nearlyTwoToMinus_54()
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
unsigned long engineIDulong()