40 #include "CLHEP/Random/TripleRand.h"
41 #include "CLHEP/Random/defs.h"
42 #include "CLHEP/Random/engineIDulong.h"
47 static const int MarkerLen = 64;
54 int TripleRand::numEngines = 0;
60 tausworthe (1234567 + numEngines + 175321),
61 integerCong(69607 * tausworthe + 54329, numEngines),
62 hurd(19781127 + integerCong)
70 tausworthe ((unsigned int)seed + 175321),
71 integerCong(69607 * tausworthe + 54329, 1313),
72 hurd(19781127 + integerCong)
85 tausworthe (rowIndex + numEngines * colIndex + 175321),
86 integerCong(69607 * tausworthe + 54329, 19),
87 hurd(19781127 + integerCong)
95 unsigned int ic ( integerCong );
96 unsigned int t ( tausworthe );
97 unsigned int h ( hurd );
105 for (
int i = 0; i < size; ++i) {
112 tausworthe = Tausworthe((
unsigned int)seed + numEngines + 175321);
113 integerCong = IntegerCong(69607 * tausworthe + 54329, numEngines);
118 setSeed(seeds ? *seeds : 1234567, 0);
123 std::ofstream outFile(filename, std::ios::out);
124 if (!outFile.bad()) {
126 std::vector<unsigned long> v =
put();
128 std::cout <<
"Result of v = put() is:\n";
130 for (
unsigned int i=0; i<v.size(); ++i) {
131 outFile << v[i] <<
"\n";
133 std::cout << v[i] <<
" ";
134 if (i%6==0) std::cout <<
"\n";
142 outFile << std::setprecision(20) <<
theSeed <<
" ";
143 tausworthe.put ( outFile );
144 integerCong.put( outFile);
145 outFile << ConstHurd() << std::endl;
150 std::ifstream inFile(filename, std::ios::in);
152 std::cerr <<
" -- Engine state remains unchanged\n";
156 std::vector<unsigned long> v;
161 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
162 if (ivec%3 == 0) std::cout <<
"\n";
165 inFile.clear(std::ios::badbit | inFile.rdstate());
166 std::cerr <<
"\nTripleRand 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 );
186 std::cout << std::setprecision(20) << std::endl;
187 std::cout <<
"-------- TripleRand 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 <<
"IntegerCong generator = " << std::endl;
193 integerCong.put( std::cout );
194 std::cout <<
"Hurd288Engine generator= " << std::endl << ConstHurd();
195 std::cout << std::endl <<
"-----------------------------------------"
199 TripleRand::operator float() {
201 ( ( integerCong ^ tausworthe ^ (
unsigned int)hurd ) * twoToMinus_32()
202 + nearlyTwoToMinus_54() );
206 TripleRand::operator
unsigned int() {
207 return integerCong ^ tausworthe ^ (
unsigned int)hurd;
212 const Hurd288Engine & TripleRand::ConstHurd()
const
216 char beginMarker[] =
"TripleRand-begin";
217 os << beginMarker <<
"\nUvec\n";
218 std::vector<unsigned long> v =
put();
219 for (
unsigned int i=0; i<v.size(); ++i) {
224 char endMarker[] =
"TripleRand-end";
225 int pr=os.precision(20);
226 os <<
" " << beginMarker <<
"\n";
228 tausworthe.put( os );
229 integerCong.put( os );
231 os <<
" " << endMarker <<
"\n";
238 std::vector<unsigned long> v;
239 v.push_back (engineIDulong<TripleRand>());
242 std::vector<unsigned long> vHurd = hurd.
put();
243 for (
unsigned int i = 0; i < vHurd.size(); ++i) {
244 v.push_back (vHurd[i]);
250 char beginMarker [MarkerLen];
256 if (strcmp(beginMarker,
"TripleRand-begin")) {
257 is.clear(std::ios::badbit | is.rdstate());
258 std::cerr <<
"\nInput mispositioned or"
259 <<
"\nTripleRand state description missing or"
260 <<
"\nwrong engine type found." << std::endl;
267 return "TripleRand-begin";
272 std::vector<unsigned long> v;
277 is.clear(std::ios::badbit | is.rdstate());
278 std::cerr <<
"\nTripleRand state (vector) description improper."
279 <<
"\ngetState() has failed."
280 <<
"\nInput stream is probably mispositioned now." << std::endl;
291 char endMarker [MarkerLen];
292 tausworthe.get( is );
293 integerCong.get( is );
298 if (strcmp(endMarker,
"TripleRand-end")) {
299 is.clear(std::ios::badbit | is.rdstate());
300 std::cerr <<
"\nTripleRand state description incomplete."
301 <<
"\nInput stream is probably mispositioned now." << std::endl;
308 if ((v[0] & 0xffffffffUL) != engineIDulong<TripleRand>()) {
310 "\nTripleRand get:state vector has wrong ID word - state unchanged\n";
314 std::cerr <<
"\nTripleRand get:state vector has wrong size: "
315 << v.size() <<
" - state unchanged\n";
322 std::vector<unsigned long>::const_iterator iv = v.begin()+1;
323 if (!tausworthe.get(iv))
return false;
324 if (!integerCong.get(iv))
return false;
325 std::vector<unsigned long> vHurd;
326 while (iv != v.end()) {
327 vHurd.push_back(*iv++);
329 if (!hurd.
get(vHurd)) {
331 "\nTripleRand get from vector: problem getting the hurd sub-engine state\n";
341 TripleRand::Tausworthe::Tausworthe() {
343 for (wordIndex = 1; wordIndex < 4; ++wordIndex) {
344 words[wordIndex] = 69607 * words[wordIndex-1] + 54329;
348 TripleRand::Tausworthe::Tausworthe(
unsigned int seed) {
350 for (wordIndex = 1; wordIndex < 4; ++wordIndex) {
351 words[wordIndex] = 69607 * words[wordIndex-1] + 54329;
355 TripleRand::Tausworthe::operator
unsigned int() {
401 if (wordIndex <= 0) {
402 for (wordIndex = 0; wordIndex < 4; ++wordIndex) {
403 words[wordIndex] = ( (words[(wordIndex+1) & 3] << 1 ) |
404 (words[wordIndex] >> 31) )
405 ^ ( (words[(wordIndex+1) & 3] << 31) |
406 (words[wordIndex] >> 1) );
409 return words[--wordIndex] & 0xffffffff;
412 void TripleRand::Tausworthe::put( std::ostream & os )
const {
413 char beginMarker[] =
"Tausworthe-begin";
414 char endMarker[] =
"Tausworthe-end";
416 int pr=os.precision(20);
417 os <<
" " << beginMarker <<
" ";
418 os << std::setprecision(20);
419 for (
int i = 0; i < 4; ++i) {
420 os << words[i] <<
" ";
423 os <<
" " << endMarker <<
" ";
428 void TripleRand::Tausworthe::put(std::vector<unsigned long> & v)
const {
429 for (
int i = 0; i < 4; ++i) {
430 v.push_back(static_cast<unsigned long>(words[i]));
432 v.push_back(static_cast<unsigned long>(wordIndex));
435 void TripleRand::Tausworthe::get( std::istream & is ) {
436 char beginMarker [MarkerLen];
437 char endMarker [MarkerLen];
442 if (strcmp(beginMarker,
"Tausworthe-begin")) {
443 is.clear(std::ios::badbit | is.rdstate());
444 std::cerr <<
"\nInput mispositioned or"
445 <<
"\nTausworthe state description missing or"
446 <<
"\nwrong engine type found." << std::endl;
448 for (
int i = 0; i < 4; ++i) {
455 if (strcmp(endMarker,
"Tausworthe-end")) {
456 is.clear(std::ios::badbit | is.rdstate());
457 std::cerr <<
"\nTausworthe state description incomplete."
458 <<
"\nInput stream is probably mispositioned now." << std::endl;
463 TripleRand::Tausworthe::get(std::vector<unsigned long>::const_iterator & iv){
464 for (
int i = 0; i < 4; ++i) {
475 TripleRand::IntegerCong::IntegerCong()
476 : state((unsigned int)3758656018U),
482 TripleRand::IntegerCong::IntegerCong(
unsigned int seed,
int streamNumber)
484 multiplier(65536 + 1024 + 5 + (8 * 1017 * streamNumber)),
502 TripleRand::IntegerCong::operator
unsigned int() {
503 return state = (state * multiplier + addend) & 0xffffffff;
506 void TripleRand::IntegerCong::put( std::ostream & os )
const {
507 char beginMarker[] =
"IntegerCong-begin";
508 char endMarker[] =
"IntegerCong-end";
510 int pr=os.precision(20);
511 os <<
" " << beginMarker <<
" ";
512 os << state <<
" " << multiplier <<
" " << addend;
513 os <<
" " << endMarker <<
" ";
518 void TripleRand::IntegerCong::put(std::vector<unsigned long> & v)
const {
519 v.push_back(static_cast<unsigned long>(state));
520 v.push_back(static_cast<unsigned long>(multiplier));
521 v.push_back(static_cast<unsigned long>(addend));
524 void TripleRand::IntegerCong::get( std::istream & is ) {
525 char beginMarker [MarkerLen];
526 char endMarker [MarkerLen];
531 if (strcmp(beginMarker,
"IntegerCong-begin")) {
532 is.clear(std::ios::badbit | is.rdstate());
533 std::cerr <<
"\nInput mispositioned or"
534 <<
"\nIntegerCong state description missing or"
535 <<
"\nwrong engine type found." << std::endl;
537 is >> state >> multiplier >> addend;
541 if (strcmp(endMarker,
"IntegerCong-end")) {
542 is.clear(std::ios::badbit | is.rdstate());
543 std::cerr <<
"\nIntegerCong state description incomplete."
544 <<
"\nInput stream is probably mispositioned now." << std::endl;
549 TripleRand::IntegerCong::get(std::vector<unsigned long>::const_iterator & iv) {
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
static const unsigned int VECTOR_STATE_SIZE
void setSeeds(const long *seeds, int)
void flatArray(const int size, double *vect)
virtual std::istream & getState(std::istream &is)
static double nearlyTwoToMinus_54()
std::vector< unsigned long > put() const
static std::string beginTag()
static std::string engineName()
static double twoToMinus_32()
void saveStatus(const char filename[]="TripleRand.conf") const
void restoreStatus(const char filename[]="TripleRand.conf")
virtual std::istream & get(std::istream &is)
virtual std::istream & get(std::istream &is)
static double twoToMinus_53()
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
void setSeed(long seed, int)
virtual std::ostream & put(std::ostream &os) const