28 #include "CLHEP/Random/Random.h"
29 #include "CLHEP/Random/MixMaxRng.h"
30 #include "CLHEP/Random/engineIDulong.h"
31 #include "CLHEP/Utility/atomic_int.h"
38 #include "CLHEP/Random/mixmax.h"
40 const unsigned long MASK32=0xffffffff;
46 CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);
53 MixMaxRng::MixMaxRng()
58 setSeed(static_cast<long>(numberOfEngines));
61 MixMaxRng::MixMaxRng(
long seed)
68 MixMaxRng::MixMaxRng(std::istream& is)
74 MixMaxRng::~MixMaxRng()
79 MixMaxRng::MixMaxRng(
const MixMaxRng& rng)
80 : HepRandomEngine(rng)
82 fRngState=
rng_copy( rng.fRngState->V );
83 fRngState->sumtot= rng.fRngState->sumtot;
84 fRngState->counter= rng.fRngState->counter;
87 MixMaxRng& MixMaxRng::operator=(
const MixMaxRng& rng)
91 if (
this == &rng) {
return *
this; }
95 HepRandomEngine::operator=(rng);
100 fRngState=
rng_copy( rng.fRngState->V );
101 fRngState->sumtot= rng.fRngState->sumtot;
102 fRngState->counter= rng.fRngState->counter;
107 void MixMaxRng::saveStatus(
const char filename[] )
const
110 FILE *fh= fopen(filename,
"w");
120 void MixMaxRng::restoreStatus(
const char filename[] )
125 void MixMaxRng::showStatus()
const
127 std::cout << std::endl;
128 std::cout <<
"------- MixMaxRng engine status -------" << std::endl;
130 std::cout <<
" Current state vector is:" << std::endl;
131 fRngState->fh=stdout;
133 std::cout <<
"---------------------------------------" << std::endl;
136 void MixMaxRng::setSeed(
long longSeed,
int )
138 unsigned long seed0, seed1= 0, seed2= 0, seed3= 0;
141 if(
sizeof(
long) > 4)
142 seed0=
static_cast<unsigned long>(longSeed) &
MASK32 ;
155 unsigned long seed0, seed1= 0, seed2= 0, seed3= 0;
158 seed0=
static_cast<unsigned long>(Seeds[0]) &
MASK32;
159 seed1=
static_cast<unsigned long>(Seeds[1]) &
MASK32;
164 seed0=
static_cast<unsigned long>(Seeds[0]) &
MASK32;
165 if( seedNum > 1){ seed1=
static_cast<unsigned long>(Seeds[1]) &
MASK32; }
166 if( seedNum > 2){ seed2=
static_cast<unsigned long>(Seeds[2]) &
MASK32; }
169 seed0=
static_cast<unsigned long>(Seeds[0]) &
MASK32;
170 seed1=
static_cast<unsigned long>(Seeds[1]) &
MASK32;
171 seed2=
static_cast<unsigned long>(Seeds[2]) &
MASK32;
172 seed3=
static_cast<unsigned long>(Seeds[3]) &
MASK32;
185 void MixMaxRng::flatArray(
const int size,
double* arrayDbl )
190 MixMaxRng::operator
unsigned int()
192 return static_cast<unsigned int>(
get_next(fRngState));
197 std::ostream & MixMaxRng::put ( std::ostream& os )
const
199 char beginMarker[] =
"MixMaxRng-begin";
200 char endMarker[] =
"MixMaxRng-end";
202 int pr = os.precision(24);
203 os << beginMarker <<
" ";
204 os << theSeed <<
" ";
206 os << fRngState->V[i] <<
"\n";
208 os << fRngState->counter <<
"\n";
209 os << fRngState->sumtot <<
"\n";
210 os << endMarker <<
"\n";
215 std::vector<unsigned long> MixMaxRng::put ()
const
217 std::vector<unsigned long> v;
218 v.push_back (engineIDulong<MixMaxRng>());
220 v.push_back(static_cast<unsigned long>(fRngState->V[i] &
MASK32));
222 v.push_back(static_cast<unsigned long>(fRngState->V[i] >> 32 ));
225 v.push_back(static_cast<unsigned long>(fRngState->counter));
226 v.push_back(static_cast<unsigned long>(fRngState->sumtot & MASK32));
227 v.push_back(static_cast<unsigned long>(fRngState->sumtot >> 32));
231 std::istream & MixMaxRng::get ( std::istream& is)
239 if (strcmp(beginMarker,
"MixMaxRng-begin")) {
240 is.clear(std::ios::badbit | is.rdstate());
241 std::cerr <<
"\nInput stream mispositioned or"
242 <<
"\nMixMaxRng state description missing or"
243 <<
"\nwrong engine type found." << std::endl;
249 std::string MixMaxRng::beginTag ()
251 return "MixMaxRng-begin";
254 std::istream & MixMaxRng::getState ( std::istream& is )
258 for (
int i=0; i<rng_get_N(); ++i) is >> fRngState->V[i];
259 is >> fRngState->counter;
265 if (strcmp(endMarker,
"MixMaxRng-end")) {
266 is.clear(std::ios::badbit | is.rdstate());
267 std::cerr <<
"\nMixMaxRng state description incomplete."
268 <<
"\nInput stream is probably mispositioned now.\n";
271 if ( fRngState->counter < 0 || fRngState->counter > N-1 ) {
272 std::cerr <<
"\nMixMaxRng::getState(): "
273 <<
"vector read wrong value of counter from file!"
274 <<
"\nInput stream is probably mispositioned now.\n";
278 if ( checksum != fRngState->sumtot) {
279 std::cerr <<
"\nMixMaxRng::getState(): "
280 <<
"checksum disagrees with value stored in file!"
281 <<
"\nInput stream is probably mispositioned now.\n";
287 bool MixMaxRng::get (
const std::vector<unsigned long> & v)
289 if ((v[0] & 0xffffffffUL) != engineIDulong<MixMaxRng>()) {
291 "\nMixMaxRng::get(): vector has wrong ID word - state unchanged\n";
297 bool MixMaxRng::getState (
const std::vector<unsigned long> & v)
299 if (v.size() != VECTOR_STATE_SIZE ) {
301 "\nMixMaxRng::getState(): vector has wrong length - state unchanged\n";
305 fRngState->V[i/2]= ( (v[i] &
MASK32) | ( (myuint)(v[i+1]) << 32 ) );
311 | ( (myuint)(v[2*
rng_get_N()+3]) << 32 ) ) != fRngState->sumtot) {
312 std::cerr <<
"\nMixMaxRng::getState(): vector has wrong checksum!"
313 <<
"\nInput vector is probably mispositioned now.\n";
void print_state(rng_state_t *X)
static const int MarkerLen
rng_state_t * rng_copy(myuint *Y)
myuint get_next(rng_state_t *X)
const unsigned long MASK32
int rng_free(rng_state_t *X)
double get_next_float(rng_state_t *X)
void seed_uniquestream(rng_state_t *Xin, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID)
rng_state_t * rng_alloc()
myuint precalc(rng_state_t *X)
void fill_array(rng_state_t *X, unsigned int n, double *array)
void read_state(rng_state_t *X, const char filename[])
void setSeeds(const SeedVector &sv)
Set the seeds of the current generator.