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.h"
44#include "CLHEP/Utility/atomic_int.h"
63 const int maxIndex = 215;
66static const int MarkerLen = 64;
83 int numEngines = numberOfEngines++;
84 int cycle = std::abs(
int(numEngines/maxIndex));
85 int curIndex = std::abs(
int(numEngines%maxIndex));
87 long mask = ((cycle & 0x007fffff) << 8);
100 int cycle = std::abs(
int(rowIndex/maxIndex));
101 int row = std::abs(
int(rowIndex%maxIndex));
102 int col = std::abs(
int(colIndex%2));
103 long mask = ((cycle & 0x000007ff) << 20);
105 seed = (seeds[col])^mask;
120 std::ofstream outFile( filename, std::ios::out ) ;
122 if (!outFile.bad()) {
124 std::vector<unsigned long> v =
put();
126 std::cout <<
"Result of v = put() is:\n";
128 for (
unsigned int i=0; i<v.size(); ++i) {
129 outFile << v[i] <<
"\n";
131 std::cout << v[i] <<
" ";
132 if (i%6==0) std::cout <<
"\n";
141 outFile <<
theSeed << std::endl;
142 for (
int i=0; i<97; ++i)
143 outFile << std::setprecision(20) << u[i] <<
" ";
144 outFile << std::endl;
145 outFile << std::setprecision(20) << c <<
" ";
146 outFile << std::setprecision(20) << cd <<
" ";
147 outFile << std::setprecision(20) << cm << std::endl;
148 outFile << pos << std::endl;
155 std::ifstream inFile( filename, std::ios::in);
157 std::cerr <<
" -- Engine state remains unchanged\n";
161 std::vector<unsigned long> v;
166 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
167 if (ivec%3 == 0) std::cout <<
"\n";
170 inFile.clear(std::ios::badbit | inFile.rdstate());
171 std::cerr <<
"\nJamesRandom state (vector) description improper."
172 <<
"\nrestoreStatus has failed."
173 <<
"\nInput stream is probably mispositioned now." << std::endl;
182 if (!inFile.bad() && !inFile.eof()) {
184 for (
int i=0; i<97; ++i)
186 inFile >> c; inFile >> cd; inFile >> cm;
196 std::cout << std::endl;
197 std::cout <<
"----- HepJamesRandom engine status -----" << std::endl;
198 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
199 std::cout <<
" u[] = ";
200 for (
int i=0; i<97; ++i)
201 std::cout << u[i] <<
" ";
202 std::cout << std::endl;
203 std::cout <<
" c = " << c <<
", cd = " << cd <<
", cm = " << cm
205 std::cout <<
" i97 = " << i97 <<
", u[i97] = " << u[i97] << std::endl;
206 std::cout <<
" j97 = " << j97 <<
", u[j97] = " << u[j97] << std::endl;
207 std::cout <<
"----------------------------------------" << std::endl;
223 std::cout <<
"Seed for HepJamesRandom must be non-negative\n"
224 <<
"Seed value supplied was " << seed
225 <<
"\nUsing its absolute value instead\n";
229 long ij = seed/30082;
230 long kl = seed - 30082*ij;
231 long i = (ij/177) % 177 + 2;
232 long j = ij % 177 + 2;
233 long k = (kl/169) % 178 + 1;
238 for ( n = 1 ; n < 98 ; n++ ) {
241 for ( m = 1 ; m < 25 ; m++) {
242 mm = ( ( (i*j) % 179 ) * k ) % 179;
246 l = ( 53 * l + 1 ) % 169;
247 if ( (l*mm % 64 ) >= 32 )
253 c = 362436.0 / 16777216.0;
254 cd = 7654321.0 / 16777216.0;
255 cm = 16777213.0 / 16777216.0;
264 setSeed(seeds ? *seeds : 19780503L, 0);
273 uni = u[i97] - u[j97];
274 if ( uni < 0.0 ) uni++;
277 if (i97 == 0) i97 = 96;
280 if (j97 == 0) j97 = 96;
284 if (c < 0.0) c += cm;
287 if (uni < 0.0) uni += 1.0;
288 }
while ( uni <= 0.0 || uni >= 1.0 );
298 for (i=0; i<size; ++i) {
307HepJamesRandom::operator float() {
308 return float( flat() );
311HepJamesRandom::operator
unsigned int() {
312 return ((
unsigned int)(flat() * exponent_bit_32()) & 0xffffffff ) |
313 (((
unsigned int)( u[i97] * exponent_bit_32())>>16) & 0xff);
317 char beginMarker[] =
"JamesRandom-begin";
318 os << beginMarker <<
"\nUvec\n";
319 std::vector<unsigned long> v =
put();
320 for (
unsigned int i=0; i<v.size(); ++i) {
325 char endMarker[] =
"JamesRandom-end";
327 long pr = os.precision(20);
328 os <<
" " << beginMarker <<
" ";
330 for (
int i=0; i<97; ++i) {
331 os << std::setprecision(20) << u[i] <<
"\n";
333 os << std::setprecision(20) << c <<
" ";
334 os << std::setprecision(20) << cd <<
" ";
335 os << std::setprecision(20) << cm <<
" ";
337 os << endMarker <<
"\n";
344 std::vector<unsigned long> v;
345 v.push_back (engineIDulong<HepJamesRandom>());
346 std::vector<unsigned long> t;
347 for (
int i=0; i<97; ++i) {
349 v.push_back(t[0]); v.push_back(t[1]);
352 v.push_back(t[0]); v.push_back(t[1]);
354 v.push_back(t[0]); v.push_back(t[1]);
356 v.push_back(t[0]); v.push_back(t[1]);
357 v.push_back(
static_cast<unsigned long>(j97));
363 char beginMarker [MarkerLen];
369 if (strcmp(beginMarker,
"JamesRandom-begin")) {
370 is.clear(std::ios::badbit | is.rdstate());
371 std::cerr <<
"\nInput stream mispositioned or"
372 <<
"\nJamesRandom state description missing or"
373 <<
"\nwrong engine type found." << std::endl;
380 return "JamesRandom-begin";
385 std::vector<unsigned long> v;
390 is.clear(std::ios::badbit | is.rdstate());
391 std::cerr <<
"\nJamesRandom state (vector) description improper."
392 <<
"\ngetState() has failed."
393 <<
"\nInput stream is probably mispositioned now." << std::endl;
405 char endMarker [MarkerLen];
406 for (
int i=0; i<97; ++i) {
409 is >> c; is >> cd; is >> cm;
414 if(strcmp(endMarker,
"JamesRandom-end")) {
415 is.clear(std::ios::badbit | is.rdstate());
416 std::cerr <<
"\nJamesRandom state description incomplete."
417 <<
"\nInput stream is probably mispositioned now." << std::endl;
428 if ( (v[0] & 0xffffffffUL) != engineIDulong<HepJamesRandom>()) {
430 "\nHepJamesRandom get:state vector has wrong ID word - state unchanged\n";
439 "\nHepJamesRandom get:state vector has wrong length - state unchanged\n";
442 std::vector<unsigned long> t(2);
443 for (
int i=0; i<97; ++i) {
444 t[0] = v[2*i+1]; t[1] = v[2*i+2];
#define CLHEP_ATOMIC_INT_TYPE
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)
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 const unsigned int VECTOR_STATE_SIZE
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)