40#include "CLHEP/Random/TripleRand.h"
41#include "CLHEP/Random/defs.h"
42#include "CLHEP/Random/engineIDulong.h"
43#include "CLHEP/Utility/atomic_int.h"
58static const int MarkerLen = 64;
68 numEngines(numberOfEngines++),
69 tausworthe (1234567 + numEngines + 175321),
70 integerCong(69607 * tausworthe + 54329, numEngines),
71 hurd(19781127 + integerCong)
79 tausworthe ((unsigned int)seed + 175321),
80 integerCong(69607 * tausworthe + 54329, 1313),
81 hurd(19781127 + integerCong)
95 numEngines(numberOfEngines),
96 tausworthe (rowIndex + numEngines * colIndex + 175321),
97 integerCong(69607 * tausworthe + 54329, 19),
98 hurd(19781127 + integerCong)
106 unsigned int ic ( integerCong );
107 unsigned int t ( tausworthe );
108 unsigned int h ( hurd );
116 for (
int i = 0; i < size; ++i) {
123 tausworthe = Tausworthe((
unsigned int)seed + 175321);
124 integerCong = IntegerCong(69607 * tausworthe + 54329, 1313);
129 setSeed(seeds ? *seeds : 1234567, 0);
134 std::ofstream outFile(filename, std::ios::out);
135 if (!outFile.bad()) {
137 std::vector<unsigned long> v =
put();
139 std::cout <<
"Result of v = put() is:\n";
141 for (
unsigned int i=0; i<v.size(); ++i) {
142 outFile << v[i] <<
"\n";
144 std::cout << v[i] <<
" ";
145 if (i%6==0) std::cout <<
"\n";
153 outFile << std::setprecision(20) <<
theSeed <<
" ";
154 tausworthe.put ( outFile );
155 integerCong.put( outFile);
156 outFile << ConstHurd() << std::endl;
161 std::ifstream inFile(filename, std::ios::in);
163 std::cerr <<
" -- Engine state remains unchanged\n";
167 std::vector<unsigned long> v;
172 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
173 if (ivec%3 == 0) std::cout <<
"\n";
176 inFile.clear(std::ios::badbit | inFile.rdstate());
177 std::cerr <<
"\nTripleRand state (vector) description improper."
178 <<
"\nrestoreStatus has failed."
179 <<
"\nInput stream is probably mispositioned now." << std::endl;
190 tausworthe.get ( inFile );
191 integerCong.get( inFile );
197 std::cout << std::setprecision(20) << std::endl;
198 std::cout <<
"-------- TripleRand engine status ---------"
200 std::cout <<
"Initial seed = " <<
theSeed << std::endl;
201 std::cout <<
"Tausworthe generator = " << std::endl;
202 tausworthe.put( std::cout );
203 std::cout <<
"IntegerCong generator = " << std::endl;
204 integerCong.put( std::cout );
205 std::cout <<
"Hurd288Engine generator= " << std::endl << ConstHurd();
206 std::cout << std::endl <<
"-----------------------------------------"
214TripleRand::operator float() {
216 ( ( integerCong ^ tausworthe ^ (
unsigned int)hurd ) * twoToMinus_32()
217 + nearlyTwoToMinus_54() );
221TripleRand::operator
unsigned int() {
222 return integerCong ^ tausworthe ^ (
unsigned int)hurd;
227const Hurd288Engine & TripleRand::ConstHurd()
const
231 char beginMarker[] =
"TripleRand-begin";
232 os << beginMarker <<
"\nUvec\n";
233 std::vector<unsigned long> v =
put();
234 for (
unsigned int i=0; i<v.size(); ++i) {
239 char endMarker[] =
"TripleRand-end";
240 int pr=os.precision(20);
241 os <<
" " << beginMarker <<
"\n";
243 tausworthe.put( os );
244 integerCong.put( os );
246 os <<
" " << endMarker <<
"\n";
253 std::vector<unsigned long> v;
254 v.push_back (engineIDulong<TripleRand>());
257 std::vector<unsigned long> vHurd = hurd.
put();
258 for (
unsigned int i = 0; i < vHurd.size(); ++i) {
259 v.push_back (vHurd[i]);
265 char beginMarker [MarkerLen];
271 if (strcmp(beginMarker,
"TripleRand-begin")) {
272 is.clear(std::ios::badbit | is.rdstate());
273 std::cerr <<
"\nInput mispositioned or"
274 <<
"\nTripleRand state description missing or"
275 <<
"\nwrong engine type found." << std::endl;
282 return "TripleRand-begin";
287 std::vector<unsigned long> v;
292 is.clear(std::ios::badbit | is.rdstate());
293 std::cerr <<
"\nTripleRand state (vector) description improper."
294 <<
"\ngetState() has failed."
295 <<
"\nInput stream is probably mispositioned now." << std::endl;
306 char endMarker [MarkerLen];
307 tausworthe.get( is );
308 integerCong.get( is );
313 if (strcmp(endMarker,
"TripleRand-end")) {
314 is.clear(std::ios::badbit | is.rdstate());
315 std::cerr <<
"\nTripleRand state description incomplete."
316 <<
"\nInput stream is probably mispositioned now." << std::endl;
323 if ((v[0] & 0xffffffffUL) != engineIDulong<TripleRand>()) {
325 "\nTripleRand get:state vector has wrong ID word - state unchanged\n";
329 std::cerr <<
"\nTripleRand get:state vector has wrong size: "
330 << v.size() <<
" - state unchanged\n";
337 std::vector<unsigned long>::const_iterator iv = v.begin()+1;
338 if (!tausworthe.get(iv))
return false;
339 if (!integerCong.get(iv))
return false;
340 std::vector<unsigned long> vHurd;
341 while (iv != v.end()) {
342 vHurd.push_back(*iv++);
344 if (!hurd.
get(vHurd)) {
346 "\nTripleRand get from vector: problem getting the hurd sub-engine state\n";
356TripleRand::Tausworthe::Tausworthe() {
358 for (wordIndex = 1; wordIndex < 4; ++wordIndex) {
359 words[wordIndex] = 69607 * words[wordIndex-1] + 54329;
363TripleRand::Tausworthe::Tausworthe(
unsigned int seed) {
365 for (wordIndex = 1; wordIndex < 4; ++wordIndex) {
366 words[wordIndex] = 69607 * words[wordIndex-1] + 54329;
370TripleRand::Tausworthe::operator
unsigned int() {
416 if (wordIndex <= 0) {
417 for (wordIndex = 0; wordIndex < 4; ++wordIndex) {
418 words[wordIndex] = ( (words[(wordIndex+1) & 3] << 1 ) |
419 (words[wordIndex] >> 31) )
420 ^ ( (words[(wordIndex+1) & 3] << 31) |
421 (words[wordIndex] >> 1) );
424 return words[--wordIndex] & 0xffffffff;
427void TripleRand::Tausworthe::put( std::ostream & os )
const {
428 char beginMarker[] =
"Tausworthe-begin";
429 char endMarker[] =
"Tausworthe-end";
431 long pr=os.precision(20);
432 os <<
" " << beginMarker <<
" ";
433 os << std::setprecision(20);
434 for (
int i = 0; i < 4; ++i) {
435 os << words[i] <<
" ";
438 os <<
" " << endMarker <<
" ";
443void TripleRand::Tausworthe::put(std::vector<unsigned long> & v)
const {
444 for (
int i = 0; i < 4; ++i) {
445 v.push_back(
static_cast<unsigned long>(words[i]));
447 v.push_back(
static_cast<unsigned long>(wordIndex));
450void TripleRand::Tausworthe::get( std::istream & is ) {
451 char beginMarker [MarkerLen];
452 char endMarker [MarkerLen];
457 if (strcmp(beginMarker,
"Tausworthe-begin")) {
458 is.clear(std::ios::badbit | is.rdstate());
459 std::cerr <<
"\nInput mispositioned or"
460 <<
"\nTausworthe state description missing or"
461 <<
"\nwrong engine type found." << std::endl;
463 for (
int i = 0; i < 4; ++i) {
470 if (strcmp(endMarker,
"Tausworthe-end")) {
471 is.clear(std::ios::badbit | is.rdstate());
472 std::cerr <<
"\nTausworthe state description incomplete."
473 <<
"\nInput stream is probably mispositioned now." << std::endl;
478TripleRand::Tausworthe::get(std::vector<unsigned long>::const_iterator & iv){
479 for (
int i = 0; i < 4; ++i) {
480 words[i] = (
unsigned int)*iv++;
482 wordIndex = (int)*iv++;
490TripleRand::IntegerCong::IntegerCong()
491: state((unsigned int)3758656018U),
497TripleRand::IntegerCong::IntegerCong(
unsigned int seed,
int streamNumber)
499 multiplier(65536 + 1024 + 5 + (8 * 1017 * streamNumber)),
517TripleRand::IntegerCong::operator
unsigned int() {
518 return state = (state * multiplier + addend) & 0xffffffff;
521void TripleRand::IntegerCong::put( std::ostream & os )
const {
522 char beginMarker[] =
"IntegerCong-begin";
523 char endMarker[] =
"IntegerCong-end";
525 long pr=os.precision(20);
526 os <<
" " << beginMarker <<
" ";
527 os << state <<
" " << multiplier <<
" " << addend;
528 os <<
" " << endMarker <<
" ";
533void TripleRand::IntegerCong::put(std::vector<unsigned long> & v)
const {
534 v.push_back(
static_cast<unsigned long>(state));
535 v.push_back(
static_cast<unsigned long>(multiplier));
536 v.push_back(
static_cast<unsigned long>(addend));
539void TripleRand::IntegerCong::get( std::istream & is ) {
540 char beginMarker [MarkerLen];
541 char endMarker [MarkerLen];
546 if (strcmp(beginMarker,
"IntegerCong-begin")) {
547 is.clear(std::ios::badbit | is.rdstate());
548 std::cerr <<
"\nInput mispositioned or"
549 <<
"\nIntegerCong state description missing or"
550 <<
"\nwrong engine type found." << std::endl;
552 is >> state >> multiplier >> addend;
556 if (strcmp(endMarker,
"IntegerCong-end")) {
557 is.clear(std::ios::badbit | is.rdstate());
558 std::cerr <<
"\nIntegerCong state description incomplete."
559 <<
"\nInput stream is probably mispositioned now." << std::endl;
564TripleRand::IntegerCong::get(std::vector<unsigned long>::const_iterator & iv) {
565 state = (
unsigned int)*iv++;
566 multiplier = (
unsigned int)*iv++;
567 addend = (
unsigned int)*iv++;
#define CLHEP_ATOMIC_INT_TYPE
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)
virtual std::ostream & put(std::ostream &os) const
virtual std::istream & get(std::istream &is)
virtual std::istream & get(std::istream &is)
static const unsigned int VECTOR_STATE_SIZE
virtual std::istream & getState(std::istream &is)
std::vector< unsigned long > put() const
void flatArray(const int size, double *vect)
void saveStatus(const char filename[]="TripleRand.conf") const
void setSeed(long seed, int)
void restoreStatus(const char filename[]="TripleRand.conf")
void setSeeds(const long *seeds, int)
static std::string engineName()
static std::string beginTag()
bool possibleKeywordInput(IS &is, const std::string &key, T &t)