39 #include "CLHEP/Random/Random.h"
40 #include "CLHEP/Random/JamesRandom.h"
41 #include "CLHEP/Random/engineIDulong.h"
42 #include "CLHEP/Random/DoubConv.h"
43 #include "CLHEP/Utility/atomic_int.h"
53 CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);
56 const int maxIndex = 215;
63 HepJamesRandom::HepJamesRandom(
long seed)
70 HepJamesRandom::HepJamesRandom()
76 int numEngines = numberOfEngines++;
77 int cycle = std::abs(
int(numEngines/maxIndex));
78 int curIndex = std::abs(
int(numEngines%maxIndex));
80 long mask = ((cycle & 0x007fffff) << 8);
81 HepRandom::getTheTableSeeds( seeds, curIndex );
87 HepJamesRandom::HepJamesRandom(
int rowIndex,
int colIndex)
93 int cycle = std::abs(
int(rowIndex/maxIndex));
94 int row = std::abs(
int(rowIndex%maxIndex));
95 int col = std::abs(
int(colIndex%2));
96 long mask = ((cycle & 0x000007ff) << 20);
97 HepRandom::getTheTableSeeds( seeds, row );
98 seed = (seeds[col])^mask;
103 HepJamesRandom::HepJamesRandom(std::istream& is)
109 HepJamesRandom::~HepJamesRandom() {}
111 void HepJamesRandom::saveStatus(
const char filename[] )
const
113 std::ofstream outFile( filename, std::ios::out ) ;
115 if (!outFile.bad()) {
117 std::vector<unsigned long> v = put();
118 for (
unsigned int i=0; i<v.size(); ++i) {
119 outFile << v[i] <<
"\n";
124 void HepJamesRandom::restoreStatus(
const char filename[] )
127 std::ifstream inFile( filename, std::ios::in);
128 if (!checkFile ( inFile, filename, engineName(),
"restoreStatus" )) {
129 std::cerr <<
" -- Engine state remains unchanged\n";
132 if ( possibleKeywordInput ( inFile,
"Uvec", theSeed ) ) {
133 std::vector<unsigned long> v;
135 for (
unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
138 inFile.clear(std::ios::badbit | inFile.rdstate());
139 std::cerr <<
"\nJamesRandom state (vector) description improper."
140 <<
"\nrestoreStatus has failed."
141 <<
"\nInput stream is probably mispositioned now." << std::endl;
150 if (!inFile.bad() && !inFile.eof()) {
152 for (
int i=0; i<97; ++i)
154 inFile >> c; inFile >>
cd; inFile >>
cm;
162 void HepJamesRandom::showStatus()
const
164 std::cout << std::endl;
165 std::cout <<
"----- HepJamesRandom engine status -----" << std::endl;
166 std::cout <<
" Initial seed = " << theSeed << std::endl;
167 std::cout <<
" u[] = ";
168 for (
int i=0; i<97; ++i)
169 std::cout << u[i] <<
" ";
170 std::cout << std::endl;
171 std::cout <<
" c = " << c <<
", cd = " <<
cd <<
", cm = " <<
cm
173 std::cout <<
" i97 = " << i97 <<
", u[i97] = " << u[i97] << std::endl;
174 std::cout <<
" j97 = " << j97 <<
", u[j97] = " << u[j97] << std::endl;
175 std::cout <<
"----------------------------------------" << std::endl;
178 void HepJamesRandom::setSeed(
long seed,
int)
191 std::cout <<
"Seed for HepJamesRandom must be non-negative\n"
192 <<
"Seed value supplied was " << seed
193 <<
"\nUsing its absolute value instead\n";
197 long ij = seed/30082;
198 long kl = seed - 30082*ij;
199 long i = (ij/177) % 177 + 2;
200 long j = ij % 177 + 2;
201 long k = (kl/169) % 178 + 1;
206 for ( n = 1 ; n < 98 ; n++ ) {
209 for ( m = 1 ; m < 25 ; m++) {
210 mm = ( ( (i*j) % 179 ) * k ) % 179;
214 l = ( 53 * l + 1 ) % 169;
215 if ( (l*mm % 64 ) >= 32 )
221 c = 362436.0 / 16777216.0;
222 cd = 7654321.0 / 16777216.0;
223 cm = 16777213.0 / 16777216.0;
232 setSeed(seeds ? *seeds : 19780503
L, 0);
241 uni = u[i97] - u[j97];
242 if ( uni < 0.0 ) uni++;
245 if (i97 == 0) i97 = 96;
248 if (j97 == 0) j97 = 96;
252 if (c < 0.0) c +=
cm;
255 if (uni < 0.0) uni += 1.0;
256 }
while ( uni <= 0.0 || uni >= 1.0 );
261 void HepJamesRandom::flatArray(
const int size,
double* vect)
266 for (i=0; i<size; ++i) {
271 HepJamesRandom::operator
unsigned int() {
272 return ((
unsigned int)(
flat() * exponent_bit_32()) & 0xffffffff ) |
273 (((
unsigned int)( u[i97] * exponent_bit_32())>>16) & 0xff);
276 std::ostream & HepJamesRandom::put ( std::ostream& os )
const {
277 char beginMarker[] =
"JamesRandom-begin";
278 os << beginMarker <<
"\nUvec\n";
279 std::vector<unsigned long> v = put();
280 for (
unsigned int i=0; i<v.size(); ++i) {
286 std::vector<unsigned long> HepJamesRandom::put ()
const {
287 std::vector<unsigned long> v;
288 v.push_back (engineIDulong<HepJamesRandom>());
289 std::vector<unsigned long> t;
290 for (
int i=0; i<97; ++i) {
291 t = DoubConv::dto2longs(u[i]);
292 v.push_back(t[0]); v.push_back(t[1]);
294 t = DoubConv::dto2longs(c);
295 v.push_back(t[0]); v.push_back(t[1]);
296 t = DoubConv::dto2longs(
cd);
297 v.push_back(t[0]); v.push_back(t[1]);
298 t = DoubConv::dto2longs(
cm);
299 v.push_back(t[0]); v.push_back(t[1]);
300 v.push_back(static_cast<unsigned long>(j97));
305 std::istream & HepJamesRandom::get ( std::istream& is) {
312 if (strcmp(beginMarker,
"JamesRandom-begin")) {
313 is.clear(std::ios::badbit | is.rdstate());
314 std::cerr <<
"\nInput stream mispositioned or"
315 <<
"\nJamesRandom state description missing or"
316 <<
"\nwrong engine type found." << std::endl;
322 std::string HepJamesRandom::beginTag ( ) {
323 return "JamesRandom-begin";
326 std::istream & HepJamesRandom::getState ( std::istream& is) {
327 if ( possibleKeywordInput ( is,
"Uvec", theSeed ) ) {
328 std::vector<unsigned long> v;
330 for (
unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
333 is.clear(std::ios::badbit | is.rdstate());
334 std::cerr <<
"\nJamesRandom state (vector) description improper."
335 <<
"\ngetState() has failed."
336 <<
"\nInput stream is probably mispositioned now." << std::endl;
349 for (
int i=0; i<97; ++i) {
352 is >> c; is >>
cd; is >>
cm;
357 if(strcmp(endMarker,
"JamesRandom-end")) {
358 is.clear(std::ios::badbit | is.rdstate());
359 std::cerr <<
"\nJamesRandom state description incomplete."
360 <<
"\nInput stream is probably mispositioned now." << std::endl;
370 bool HepJamesRandom::get (
const std::vector<unsigned long> & v) {
371 if ( (v[0] & 0xffffffffUL) != engineIDulong<HepJamesRandom>()) {
373 "\nHepJamesRandom get:state vector has wrong ID word - state unchanged\n";
379 bool HepJamesRandom::getState (
const std::vector<unsigned long> & v) {
380 if (v.size() != VECTOR_STATE_SIZE ) {
382 "\nHepJamesRandom get:state vector has wrong length - state unchanged\n";
385 std::vector<unsigned long> t(2);
386 for (
int i=0; i<97; ++i) {
387 t[0] = v[2*i+1]; t[1] = v[2*i+2];
388 u[i] = DoubConv::longs2double(t);
390 t[0] = v[195]; t[1] = v[196]; c = DoubConv::longs2double(t);
391 t[0] = v[197]; t[1] = v[198]; cd = DoubConv::longs2double(t);
392 t[0] = v[199]; t[1] = v[200]; cm = DoubConv::longs2double(t);
static const int MarkerLen
void setSeeds(const SeedVector &sv)
Set the seeds of the current generator.