33#include "CLHEP/Random/defs.h"
34#include "CLHEP/Random/Random.h"
35#include "CLHEP/Random/DRand48Engine.h"
36#include "CLHEP/Random/RandomFunc.h"
37#include "CLHEP/Random/engineIDulong.h"
50static const int MarkerLen = 64;
52int DRand48Engine::numEngines = 0;
57const int DRand48Engine::maxIndex = 215;
72 int cycle = abs(
int(numEngines/maxIndex));
73 int curIndex = abs(
int(numEngines%maxIndex));
75 long mask = ((cycle & 0x007fffff) << 8);
88 int cycle = abs(
int(rowIndex/maxIndex));
89 int row = abs(
int(rowIndex%maxIndex));
90 int col = abs(
int(colIndex%2));
91 long mask = ((cycle & 0x000007ff) << 20);
93 seed = (seeds[col])^mask;
114 setSeed(seeds ? *seeds : 19780503L, 0);
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 unsigned short dummy[] = { 0, 0, 0 };
142 unsigned short* cseed =
seed48(dummy);
143 if (!outFile.bad()) {
144 outFile <<
theSeed << std::endl;
145 for (
int i=0; i<3; ++i) {
146 outFile << cseed[i] << std::endl;
156 std::ifstream inFile( filename, std::ios::in);
157 unsigned short cseed[3];
160 std::cerr <<
" -- Engine state remains unchanged\n";
164 std::vector<unsigned long> v;
169 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
170 if (ivec%3 == 0) std::cout <<
"\n";
173 inFile.clear(std::ios::badbit | inFile.rdstate());
174 std::cerr <<
"\nDRand48Engine state (vector) description improper."
175 <<
"\nrestoreStatus has failed."
176 <<
"\nInput stream is probably mispositioned now." << std::endl;
185 if (!inFile.bad() && !inFile.eof()) {
187 for (
int i=0; i<3; ++i)
195 unsigned short dummy[] = { 0, 0, 0 };
196 unsigned short* cseed =
seed48(dummy);
197 std::cout << std::endl;
198 std::cout <<
"-------- DRand48 engine status ---------" << std::endl;
199 std::cout <<
" Initial seed = " <<
theSeed << std::endl;
200 std::cout <<
" Current seeds = " << cseed[0] <<
", ";
201 std::cout << cseed[1] <<
", ";
202 std::cout << cseed[2] << std::endl;
203 std::cout <<
"----------------------------------------" << std::endl;
204 for (
int i=0; i<3; ++i)
222 for (i=0; i<size; ++i)
228 char beginMarker[] =
"DRand48Engine-begin";
229 os << beginMarker <<
"\nUvec\n";
230 std::vector<unsigned long> v =
put();
231 for (
unsigned int i=0; i<v.size(); ++i) {
237 unsigned short dummy[] = { 0, 0, 0 };
238 unsigned short* cseed =
seed48(dummy);
239 char endMarker[] =
"DRand48Engine-end";
240 os <<
" " << beginMarker <<
" ";
242 for (
int i=0; i<3; ++i) {
244 os << cseed[i] <<
" ";
246 os << endMarker <<
" ";
253 std::vector<unsigned long> v;
254 v.push_back (engineIDulong<DRand48Engine>());
255 unsigned short dummy[] = { 0, 0, 0 };
256 unsigned short* cseed =
seed48(dummy);
257 for (
int i=0; i<3; ++i) {
259 v.push_back (
static_cast<unsigned long>(cseed[i]));
267 char beginMarker [MarkerLen];
273 if (strcmp(beginMarker,
"DRand48Engine-begin")) {
274 is.clear(std::ios::badbit | is.rdstate());
275 std::cerr <<
"\nInput stream mispositioned or"
276 <<
"\nDRand48Engine state description missing or"
277 <<
"\nwrong engine type found." << std::endl;
284 return "DRand48Engine-begin";
289 unsigned short cseed[3];
291 std::vector<unsigned long> v;
294 std::cout <<
"DRand48Engine::getState detected Uvec keyword\n";
301 std::cout <<
"ivec = " << ivec <<
" uu = " << uu <<
"\n";
304 is.clear(std::ios::badbit | is.rdstate());
305 std::cerr <<
"\nDRand48Engine state (vector) description improper."
306 <<
"\ngetState() has failed."
307 <<
"\nInput stream is probably mispositioned now." << std::endl;
318 char endMarker [MarkerLen];
320 for (
int i=1; i<3; ++i) {
326 if (strcmp(endMarker,
"DRand48Engine-end")) {
327 is.clear(std::ios::badbit | is.rdstate());
328 std::cerr <<
"\nDRand48Engine state description incomplete."
329 <<
"\nInput stream is probably mispositioned now." << std::endl;
337 if ((v[0] & 0xffffffffUL) != engineIDulong<DRand48Engine>()) {
339 "\nDRand48Engine get:state vector has wrong ID word - state unchanged\n";
348 "\nDRand48Engine getState:state vector has wrong length - state unchanged\n";
351 unsigned short cseed[3];
352 for (
int i=0; i<3; ++i) {
353 cseed[i] =
static_cast<unsigned short>(v[i+1]);
unsigned short * seed48(unsigned short int[3])
virtual std::istream & getState(std::istream &is)
static std::string engineName()
static std::string beginTag()
void setSeed(long seed, int dum=0)
static const unsigned int VECTOR_STATE_SIZE
void setSeeds(const long *seeds, int dum=0)
virtual std::istream & get(std::istream &is)
void saveStatus(const char filename[]="DRand48.conf") const
std::vector< unsigned long > put() const
void restoreStatus(const char filename[]="DRand48.conf")
void flatArray(const int size, double *vect)
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)