75 const int maxIndex = 215;
78static const int MarkerLen = 64;
84template< std::size_t
n,
85 bool =
n < std::size_t(std::numeric_limits<unsigned long>::digits) >
86 struct do_right_shift;
87template< std::
size_t n >
88 struct do_right_shift<n,true>
90 unsigned long operator()(
unsigned long value) {
return value >>
n; }
92template< std::
size_t n >
93 struct do_right_shift<
n,false>
98template< std::
size_t nbits >
99 unsigned long rshift(
unsigned long value )
100{
return do_right_shift<nbits>()(value); }
111 int numEngines = numberOfEngines++;
112 int cycle = std::abs(
int(numEngines/maxIndex));
113 int curIndex = std::abs(
int(numEngines%maxIndex));
115 long mask = ((cycle & 0x007fffff) << 8);
131 long seedlist[2]={seed,0};
133 advance ( 2*lux + 1 );
141 int cycle = std::abs(
int(rowIndex/maxIndex));
142 int row = std::abs(
int(rowIndex%maxIndex));
143 long mask = (( cycle & 0x000007ff ) << 20 );
165 if (index <= 0) update();
169void Ranlux64Engine::update() {
198 if ( endIters == 1 ) {
199 y1 = randoms[ 4] - randoms[11] - carry;
206 randoms[11] = randoms[10];
207 randoms[10] = randoms[ 9];
208 randoms[ 9] = randoms[ 8];
209 randoms[ 8] = randoms[ 7];
210 randoms[ 7] = randoms[ 6];
211 randoms[ 6] = randoms[ 5];
212 randoms[ 5] = randoms[ 4];
213 randoms[ 4] = randoms[ 3];
214 randoms[ 3] = randoms[ 2];
215 randoms[ 2] = randoms[ 1];
216 randoms[ 1] = randoms[ 0];
222 for ( m = 0, nr = 11, ns = 4; m < endIters; ++m, --nr ) {
223 y1 = randoms [
ns] - randoms[nr] - carry;
238 for (m=0; m<12; m++) {
243 for (m=11; m>=0; --m) {
244 randoms[m] = temp[
ns];
259void Ranlux64Engine::advance(
int dozens) {
285 for ( k = dozens; k > 0; --k ) {
287 y1 = randoms[ 4] - randoms[11] - carry;
289 y2 = randoms[ 3] - randoms[10];
296 y3 = randoms[ 2] - randoms[ 9];
303 y1 = randoms[ 1] - randoms[ 8];
310 y2 = randoms[ 0] - randoms[ 7];
317 y3 = randoms[11] - randoms[ 6];
324 y1 = randoms[10] - randoms[ 5];
331 y2 = randoms[ 9] - randoms[ 4];
338 y3 = randoms[ 8] - randoms[ 3];
345 y1 = randoms[ 7] - randoms[ 2];
352 y2 = randoms[ 6] - randoms[ 1];
359 y3 = randoms[ 5] - randoms[ 0];
377 for(
int i=0; i < size; ++i ) {
390 const int ecuyer_a(53668);
391 const int ecuyer_b(40014);
392 const int ecuyer_c(12211);
393 const int ecuyer_d(2147483563);
395 const int lux_levels[3] = {109, 202, 397};
398 if( (lux > 2)||(lux < 0) ){
399 pDiscard = (lux >= 12) ? (lux-12) : lux_levels[1];
401 pDiscard = lux_levels[luxury];
403 pDozens = pDiscard / 12;
404 endIters = pDiscard % 12;
407 long next_seed = seed;
410 next_seed &= 0xffffffff;
411 while( next_seed >= ecuyer_d ) {
412 next_seed -= ecuyer_d;
415 for(i = 0;i != 24;i++){
416 k_multiple = next_seed / ecuyer_a;
417 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
418 - k_multiple * ecuyer_c;
420 next_seed += ecuyer_d;
422 next_seed &= 0xffffffff;
423 init_table[i] = next_seed;
426 if(
sizeof(
long) >= 8 ) {
427 int64_t topbits1, topbits2;
429 topbits1 = ( (int64_t) seed >> 32) & 0xffff ;
430 topbits2 = ( (int64_t) seed >> 48) & 0xffff ;
432 topbits1 = detail::rshift<32>(seed) & 0xffff ;
433 topbits2 = detail::rshift<48>(seed) & 0xffff ;
435 init_table[0] ^= topbits1;
436 init_table[2] ^= topbits2;
441 for(i = 0;i < 12; i++){
465 const int ecuyer_a = 53668;
466 const int ecuyer_b = 40014;
467 const int ecuyer_c = 12211;
468 const int ecuyer_d = 2147483563;
470 const int lux_levels[3] = {109, 202, 397};
487 if( (lux > 2)||(lux < 0) ){
488 pDiscard = (lux >= 12) ? (lux-12) : lux_levels[1];
490 pDiscard = lux_levels[luxury];
492 pDozens = pDiscard / 12;
493 endIters = pDiscard % 12;
496 long next_seed = *seeds;
500 for( i = 0;(i != 24)&&(*seedptr != 0);i++){
501 init_table[i] = *seedptr & 0xffffffff;
506 next_seed = init_table[i-1];
508 k_multiple = next_seed / ecuyer_a;
509 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
510 - k_multiple * ecuyer_c;
512 next_seed += ecuyer_d;
514 next_seed &= 0xffffffff;
515 init_table[i] = next_seed;
519 for(i = 0;i < 12; i++){
532 std::ofstream outFile( filename, std::ios::out ) ;
533 if (!outFile.bad()) {
535 std::vector<unsigned long> v =
put();
536 for (
unsigned int i=0; i<v.size(); ++i) {
537 outFile << v[i] <<
"\n";
544 std::ifstream inFile( filename, std::ios::in);
546 std::cerr <<
" -- Engine state remains unchanged\n";
550 std::vector<unsigned long> v;
555 inFile.clear(std::ios::badbit | inFile.rdstate());
556 std::cerr <<
"\nJamesRandom state (vector) description improper."
557 <<
"\nrestoreStatus has failed."
558 <<
"\nInput stream is probably mispositioned now." << std::endl;
567 if (!inFile.bad() && !inFile.eof()) {
569 for (
int i=0; i<12; ++i) {
570 inFile >> randoms[i];
572 inFile >> carry; inFile >> index;
573 inFile >> luxury; inFile >> pDiscard;
574 pDozens = pDiscard / 12;
575 endIters = pDiscard % 12;
581 std::cout << std::endl;
582 std::cout <<
"--------- Ranlux engine status ---------" << std::endl;
583 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
584 std::cout <<
" randoms[] = ";
585 for (
int i=0; i<12; ++i) {
586 std::cout << randoms[i] << std::endl;
588 std::cout << std::endl;
589 std::cout <<
" carry = " << carry <<
", index = " << index << std::endl;
590 std::cout <<
" luxury = " << luxury <<
" pDiscard = "
591 << pDiscard << std::endl;
592 std::cout <<
"----------------------------------------" << std::endl;
597 char beginMarker[] =
"Ranlux64Engine-begin";
598 os << beginMarker <<
"\nUvec\n";
599 std::vector<unsigned long> v =
put();
600 for (
unsigned int i=0; i<v.size(); ++i) {
607 std::vector<unsigned long> v;
608 v.push_back (engineIDulong<Ranlux64Engine>());
609 std::vector<unsigned long> t;
610 for (
int i=0; i<12; ++i) {
612 v.push_back(t[0]); v.push_back(t[1]);
615 v.push_back(t[0]); v.push_back(t[1]);
616 v.push_back(
static_cast<unsigned long>(index));
617 v.push_back(
static_cast<unsigned long>(luxury));
618 v.push_back(
static_cast<unsigned long>(pDiscard));
624 char beginMarker [MarkerLen];
630 if (strcmp(beginMarker,
"Ranlux64Engine-begin")) {
631 is.clear(std::ios::badbit | is.rdstate());
632 std::cerr <<
"\nInput stream mispositioned or"
633 <<
"\nRanlux64Engine state description missing or"
634 <<
"\nwrong engine type found." << std::endl;
641 return "Ranlux64Engine-begin";
647 std::vector<unsigned long> v;
652 is.clear(std::ios::badbit | is.rdstate());
653 std::cerr <<
"\nRanlux64Engine state (vector) description improper."
654 <<
"\ngetState() has failed."
655 <<
"\nInput stream is probably mispositioned now." << std::endl;
666 char endMarker [MarkerLen];
667 for (
int i=0; i<12; ++i) {
670 is >> carry; is >> index;
671 is >> luxury; is >> pDiscard;
672 pDozens = pDiscard / 12;
673 endIters = pDiscard % 12;
677 if (strcmp(endMarker,
"Ranlux64Engine-end")) {
678 is.clear(std::ios::badbit | is.rdstate());
679 std::cerr <<
"\nRanlux64Engine state description incomplete."
680 <<
"\nInput stream is probably mispositioned now." << std::endl;
687 if ((v[0] & 0xffffffffUL) != engineIDulong<Ranlux64Engine>()) {
689 "\nRanlux64Engine get:state vector has wrong ID word - state unchanged\n";
698 "\nRanlux64Engine get:state vector has wrong length - state unchanged\n";
701 std::vector<unsigned long> t(2);
702 for (
int i=0; i<12; ++i) {
703 t[0] = v[2*i+1]; t[1] = v[2*i+2];
706 t[0] = v[25]; t[1] = v[26];
#define CLHEP_ATOMIC_INT_TYPE
static double longs2double(const std::vector< unsigned long > &v)
static std::vector< unsigned long > dto2longs(double d)
static double twoToMinus_32()
static double twoToMinus_49()
static double twoToMinus_48()
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 setSeeds(const long *seeds, int lux=1)
void restoreStatus(const char filename[]="Ranlux64.conf")
std::vector< unsigned long > put() const
virtual std::istream & getState(std::istream &is)
static std::string engineName()
void saveStatus(const char filename[]="Ranlux64.conf") const
static std::string beginTag()
void flatArray(const int size, double *vect)
void setSeed(long seed, int lux=1)
static const unsigned int VECTOR_STATE_SIZE
virtual std::istream & get(std::istream &is)
virtual ~Ranlux64Engine()
unsigned long rshift(unsigned long value)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
unsigned long operator()(unsigned long)