39#include "CLHEP/Random/defs.h"
40#include "CLHEP/Random/Random.h"
41#include "CLHEP/Random/JamesRandom.h"
42#include "CLHEP/Random/engineIDulong.h"
43#include "CLHEP/Random/DoubConv.hh"
52static const int MarkerLen = 64;
57int HepJamesRandom::numEngines = 0;
60int HepJamesRandom::maxIndex = 215;
75 int cycle = std::abs(
int(numEngines/maxIndex));
76 int curIndex = std::abs(
int(numEngines%maxIndex));
78 long mask = ((cycle & 0x007fffff) << 8);
91 int cycle = std::abs(
int(rowIndex/maxIndex));
92 int row = std::abs(
int(rowIndex%maxIndex));
93 int col = std::abs(
int(colIndex%2));
94 long mask = ((cycle & 0x000007ff) << 20);
96 seed = (seeds[col])^mask;
111 std::ofstream outFile( filename, std::ios::out ) ;
113 if (!outFile.bad()) {
115 std::vector<unsigned long> v =
put();
117 std::cout <<
"Result of v = put() is:\n";
119 for (
unsigned int i=0; i<v.size(); ++i) {
120 outFile << v[i] <<
"\n";
122 std::cout << v[i] <<
" ";
123 if (i%6==0) std::cout <<
"\n";
132 outFile <<
theSeed << std::endl;
133 for (
int i=0; i<97; ++i)
134 outFile << std::setprecision(20) << u[i] <<
" ";
135 outFile << std::endl;
136 outFile << std::setprecision(20) << c <<
" ";
137 outFile << std::setprecision(20) << cd <<
" ";
138 outFile << std::setprecision(20) << cm << std::endl;
139 outFile << pos << std::endl;
146 std::ifstream inFile( filename, std::ios::in);
148 std::cerr <<
" -- Engine state remains unchanged\n";
152 std::vector<unsigned long> v;
157 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
158 if (ivec%3 == 0) std::cout <<
"\n";
161 inFile.clear(std::ios::badbit | inFile.rdstate());
162 std::cerr <<
"\nJamesRandom state (vector) description improper."
163 <<
"\nrestoreStatus has failed."
164 <<
"\nInput stream is probably mispositioned now." << std::endl;
173 if (!inFile.bad() && !inFile.eof()) {
175 for (
int i=0; i<97; ++i)
177 inFile >> c; inFile >> cd; inFile >> cm;
187 std::cout << std::endl;
188 std::cout <<
"----- HepJamesRandom engine status -----" << std::endl;
189 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
190 std::cout <<
" u[] = ";
191 for (
int i=0; i<97; ++i)
192 std::cout << u[i] <<
" ";
193 std::cout << std::endl;
194 std::cout <<
" c = " << c <<
", cd = " << cd <<
", cm = " << cm
196 std::cout <<
" i97 = " << i97 <<
", u[i97] = " << u[i97] << std::endl;
197 std::cout <<
" j97 = " << j97 <<
", u[j97] = " << u[j97] << std::endl;
198 std::cout <<
"----------------------------------------" << std::endl;
214 std::cout <<
"Seed for HepJamesRandom must be non-negative\n"
215 <<
"Seed value supplied was " << seed
216 <<
"\nUsing its absolute value instead\n";
220 long ij = seed/30082;
221 long kl = seed - 30082*ij;
222 long i = (ij/177) % 177 + 2;
223 long j = ij % 177 + 2;
224 long k = (kl/169) % 178 + 1;
229 for ( n = 1 ; n < 98 ; n++ ) {
232 for ( m = 1 ; m < 25 ; m++) {
233 mm = ( ( (i*j) % 179 ) * k ) % 179;
237 l = ( 53 * l + 1 ) % 169;
238 if ( (l*mm % 64 ) >= 32 )
244 c = 362436.0 / 16777216.0;
245 cd = 7654321.0 / 16777216.0;
246 cm = 16777213.0 / 16777216.0;
255 setSeed(seeds ? *seeds : 19780503L, 0);
264 uni = u[i97] - u[j97];
265 if ( uni < 0.0 ) uni++;
268 if (i97 == 0) i97 = 96;
271 if (j97 == 0) j97 = 96;
275 if (c < 0.0) c += cm;
278 if (uni < 0.0) uni += 1.0;
279 }
while ( uni <= 0.0 || uni >= 1.0 );
289 for (i=0; i<size; ++i) {
294HepJamesRandom::operator
unsigned int() {
295 return ((
unsigned int)(flat() * exponent_bit_32()) & 0xffffffff ) |
296 (((
unsigned int)( u[i97] * exponent_bit_32())>>16) & 0xff);
300 char beginMarker[] =
"JamesRandom-begin";
301 os << beginMarker <<
"\nUvec\n";
302 std::vector<unsigned long> v =
put();
303 for (
unsigned int i=0; i<v.size(); ++i) {
308 char endMarker[] =
"JamesRandom-end";
310 int pr = os.precision(20);
311 os <<
" " << beginMarker <<
" ";
313 for (
int i=0; i<97; ++i) {
314 os << std::setprecision(20) << u[i] <<
"\n";
316 os << std::setprecision(20) << c <<
" ";
317 os << std::setprecision(20) << cd <<
" ";
318 os << std::setprecision(20) << cm <<
" ";
320 os << endMarker <<
"\n";
327 std::vector<unsigned long> v;
328 v.push_back (engineIDulong<HepJamesRandom>());
329 std::vector<unsigned long> t;
330 for (
int i=0; i<97; ++i) {
332 v.push_back(t[0]); v.push_back(t[1]);
335 v.push_back(t[0]); v.push_back(t[1]);
337 v.push_back(t[0]); v.push_back(t[1]);
339 v.push_back(t[0]); v.push_back(t[1]);
340 v.push_back(
static_cast<unsigned long>(j97));
346 char beginMarker [MarkerLen];
352 if (strcmp(beginMarker,
"JamesRandom-begin")) {
353 is.clear(std::ios::badbit | is.rdstate());
354 std::cerr <<
"\nInput stream mispositioned or"
355 <<
"\nJamesRandom state description missing or"
356 <<
"\nwrong engine type found." << std::endl;
363 return "JamesRandom-begin";
368 std::vector<unsigned long> v;
373 is.clear(std::ios::badbit | is.rdstate());
374 std::cerr <<
"\nJamesRandom state (vector) description improper."
375 <<
"\ngetState() has failed."
376 <<
"\nInput stream is probably mispositioned now." << std::endl;
388 char endMarker [MarkerLen];
389 for (
int i=0; i<97; ++i) {
392 is >> c; is >> cd; is >> cm;
397 if(strcmp(endMarker,
"JamesRandom-end")) {
398 is.clear(std::ios::badbit | is.rdstate());
399 std::cerr <<
"\nJamesRandom state description incomplete."
400 <<
"\nInput stream is probably mispositioned now." << std::endl;
411 if ( (v[0] & 0xffffffffUL) != engineIDulong<HepJamesRandom>()) {
413 "\nHepJamesRandom get:state vector has wrong ID word - state unchanged\n";
422 "\nHepJamesRandom get:state vector has wrong length - state unchanged\n";
425 std::vector<unsigned long> t(2);
426 for (
int i=0; i<97; ++i) {
427 t[0] = v[2*i+1]; t[1] = v[2*i+2];
static double longs2double(const std::vector< unsigned long > &v)
static std::vector< unsigned long > dto2longs(double d)
void flatArray(const int size, double *vect)
static std::string beginTag()
static std::string engineName()
void setSeeds(const long *seeds, int dum=0)
static const unsigned int VECTOR_STATE_SIZE
virtual std::istream & getState(std::istream &is)
virtual ~HepJamesRandom()
std::vector< unsigned long > put() const
void setSeed(long seed, int dum=0)
virtual std::istream & get(std::istream &is)
void saveStatus(const char filename[]="JamesRand.conf") const
void restoreStatus(const char filename[]="JamesRand.conf")
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)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)