48 void EGS_RandomGenerator::allocate(
int n) {
50 egsFatal(
"Attempt to construct a RNG with n < 1\n");
52 rarray =
new EGS_Float[n];
58 if (np > 0 && np != r.
np) {
69 for (
int j=ip; j<np; j++) {
77 for (
int j=0; j<
np; j++) {
86 data <<
" " <<
np <<
" " <<
ip << endl;
87 for (
int j=0; j<
np; j++) {
97 bool EGS_RandomGenerator::setState(istream &data) {
103 if (!data.good() || data.fail() || data.eof()) {
109 if (np1 !=
np &&
np > 0) {
111 rarray =
new EGS_Float [np1];
114 for (
int j=0; j<
np; j++) {
123 bool EGS_RandomGenerator::addState(istream &data) {
124 EGS_I64 count_save =
count;
125 if (!setState(data)) {
150 ix(r.ix), jx(r.jx), c(r.c), iseed1(r.iseed1), iseed2(r.iseed2),
151 high_res(r.high_res) {
152 for (
int j=0; j<97; j++) {
178 return baseSize() + 102*
sizeof(int);
181 void setHighResolution(
bool hr) {
201 for (
int j=0; j<97; j++) {
208 void setState(
int ixx,
int jxx);
213 static EGS_Float twom24;
222 void EGS_Ranmar::saveState() {
232 void EGS_Ranmar::resetState() {
244 c->copyBaseState(*
this);
252 egsFatal(
"EGS_Ranmar::setState: attampt to set my state by a non EGS_Ranmar RNG!\n");
259 for (
int j=0; j<97; j++) {
265 data << ix <<
" " << jx <<
" " << c <<
" "
266 << iseed1 <<
" " << iseed2 <<
" " << high_res << endl;
267 for (
int j=0; j<97; j++) {
275 data >> ix >> jx >> c >> iseed1 >> iseed2 >> high_res;
276 for (
int j=0; j<97; j++) {
283 EGS_Float EGS_Ranmar::twom24 = 1./16777216.;
284 int EGS_Ranmar::cd = 7654321;
285 int EGS_Ranmar::cm = 16777213;
289 "============================================\n");
302 void EGS_Ranmar::setState(
int ixx,
int jxx) {
303 if (ixx <= 0 || ixx >= 31328) {
306 if (jxx <= 0 || jxx >= 30081) {
311 int i = (ixx/177)%177 + 2;
313 int k = (jxx/169)%178 + 1;
316 for (
int ii=0; ii<97; ii++) {
319 for (
int jj=0; jj<24; jj++) {
320 int m = (((i*j)%179)*k)%179;
325 if ((l*m)%64 >= 32) {
338 for (
int ii=0; ii<n; ii++) {
339 register int r = u[ix] - u[jx--];
368 array[ii] = twom24*r;
371 for (
int ii=0; ii<n; ii++) {
372 register int r = u[ix] - u[jx--];
401 array[ii] += twom24*twom24*r;
411 egsWarning(
"EGS_RandomGenerator::createRNG: null input?\n");
415 bool delete_it =
false;
416 if (input->
isA(
"rng definition")) {
422 egsWarning(
"EGS_RandomGenerator::createRNG: no 'RNG definition'"
431 egsWarning(
"EGS_RandomGenerator::createRNG: no RNG type specified\n"
432 " Assuming ranmar.\n");
436 if (i->
compare(type,
"ranmar")) {
438 err = i->
getInput(
"initial seeds",seeds);
440 if (!err && seeds.size() == 2) {
441 res =
new EGS_Ranmar(seeds[0],seeds[1] + sequence);
446 vector<string> hr_options;
447 hr_options.push_back(
"no");
448 hr_options.push_back(
"yes");
449 bool hr = i->
getInput(
"high resolution",hr_options,0);
450 res->setHighResolution(hr);
454 egsWarning(
"EGS_RandomGenerator::createRNG: unknown RNG type %s\n",
467 int iaux = sequence/30081;
468 int jxx = sequence - iaux*30081;
471 if (iaux > 31328)
egsFatal(
"EGS_RandomGenerator::defaultRNG: "
472 "sequence %d is outside of allowed range\n",sequence);
virtual void resetState()=0
Reset the state to a previously saved state.
bool storePrivateState(ostream &data)
bool EGS_EXPORT egsGetI64(istream &data, EGS_I64 &n)
Reads a 64 bit integer from the stream data and assigns it to n. Returns true on success, false on failure.
EGS_I64 count
random number generated so far
EGS_RandomGenerator(int n=128)
Construct a RNG that has an array of n points to hold random numbers.
virtual bool setPrivateState(istream &data)=0
Set the state of the RNG object from the data in the input stream data.
bool storeState(ostream &data)
Functions for storing, seting and reseting the state of a RNG object.
virtual void saveState()=0
Save the RNG state.
bool EGS_EXPORT egsStoreI64(ostream &data, EGS_I64 n)
Writes the 64 bit integer n to the output stream data and returns true on success, false on failure.
int baseSize() const
The memory needed to store the base state.
Global egspp functions header file.
virtual EGS_RandomGenerator * getCopy()=0
Get a copy of the RNG.
virtual bool storePrivateState(ostream &data)=0
Store the state of the RNG to the output stream data.
virtual void fillArray(int n, EGS_Float *array)=0
Fill the array of n elements pointed to by array with random numbers.
void describeRNG() const
Output information about this RNG using egsInformation()
Base random number generator class. All random number generators should be derived from this class...
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
virtual int rngSize() const =0
The memory needed to store the rng state.
EGS_RandomGenerator class header file.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
virtual void describeRNG() const
Describe this RNG.
static EGS_RandomGenerator * createRNG(EGS_Input *inp, int sequence=0)
Create a RNG object from the information pointed to by inp and return a pointer to it...
bool setPrivateState(istream &data)
void fillArray(int n, EGS_Float *array)
Fill the array pointed to by array with random numbers.
int ip
pointer to the next rarray element in the sequence
static EGS_RandomGenerator * defaultRNG(int sequence=0)
Returns a pointer to the default egspp RNG.
int np
size of the array rarray
EGS_Float * rarray
array with random numbers of size np.
EGS_Ranmar(int ixx=1802, int jxx=9373, int n=128)
Construct a ranmar RNG using ixx and jxx as the initial seeds.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.