49 void EGS_RandomGenerator::allocate(
int n) {
51 egsFatal(
"Attempt to construct a RNG with n < 1\n");
53 rarray =
new EGS_Float[n];
59 if (np > 0 && np != r.
np) {
70 for (
int j=ip; j<np; j++) {
78 for (
int j=0; j<
np; j++) {
87 data <<
" " <<
np <<
" " <<
ip << endl;
88 for (
int j=0; j<
np; j++) {
98 bool EGS_RandomGenerator::setState(istream &data) {
104 if (!data.good() || data.fail() || data.eof()) {
110 if (np1 !=
np &&
np > 0) {
112 rarray =
new EGS_Float [np1];
115 for (
int j=0; j<
np; j++) {
124 bool EGS_RandomGenerator::addState(istream &data) {
125 EGS_I64 count_save =
count;
126 if (!setState(data)) {
151 ix(r.ix), jx(r.jx), c(r.c), iseed1(r.iseed1), iseed2(r.iseed2),
152 high_res(r.high_res) {
153 for (
int j=0; j<97; j++) {
179 return baseSize() + 102*
sizeof(int);
182 void setHighResolution(
bool hr) {
202 for (
int j=0; j<97; j++) {
209 void setState(
int ixx,
int jxx);
214 static EGS_Float twom24;
223 void EGS_Ranmar::saveState() {
233 void EGS_Ranmar::resetState() {
245 c->copyBaseState(*
this);
253 egsFatal(
"EGS_Ranmar::setState: attampt to set my state by a non EGS_Ranmar RNG!\n");
260 for (
int j=0; j<97; j++) {
266 data << ix <<
" " << jx <<
" " << c <<
" "
267 << iseed1 <<
" " << iseed2 <<
" " << high_res << endl;
268 for (
int j=0; j<97; j++) {
276 data >> ix >> jx >> c >> iseed1 >> iseed2 >> high_res;
277 for (
int j=0; j<97; j++) {
284 EGS_Float EGS_Ranmar::twom24 = 1./16777216.;
285 int EGS_Ranmar::cd = 7654321;
286 int EGS_Ranmar::cm = 16777213;
290 "============================================\n");
303 void EGS_Ranmar::setState(
int ixx,
int jxx) {
304 if (ixx <= 0 || ixx >= 31328) {
307 if (jxx <= 0 || jxx >= 30081) {
312 int i = (ixx/177)%177 + 2;
314 int k = (jxx/169)%178 + 1;
317 for (
int ii=0; ii<97; ii++) {
320 for (
int jj=0; jj<24; jj++) {
321 int m = (((i*j)%179)*k)%179;
326 if ((l*m)%64 >= 32) {
339 for (
int ii=0; ii<n; ii++) {
340 register int r = u[ix] - u[jx--];
369 array[ii] = twom24*r;
372 for (
int ii=0; ii<n; ii++) {
373 register int r = u[ix] - u[jx--];
402 array[ii] += twom24*twom24*r;
412 egsWarning(
"EGS_RandomGenerator::createRNG: null input?\n");
416 bool delete_it =
false;
417 if (input->
isA(
"rng definition")) {
423 egsWarning(
"EGS_RandomGenerator::createRNG: no 'RNG definition'"
432 egsWarning(
"EGS_RandomGenerator::createRNG: no RNG type specified\n"
433 " Assuming ranmar.\n");
437 if (i->
compare(type,
"ranmar")) {
439 err = i->
getInput(
"initial seeds",seeds);
441 if (!err && seeds.size() == 2) {
442 res =
new EGS_Ranmar(seeds[0],seeds[1] + sequence);
447 vector<string> hr_options;
448 hr_options.push_back(
"no");
449 hr_options.push_back(
"yes");
450 bool hr = i->
getInput(
"high resolution",hr_options,0);
451 res->setHighResolution(hr);
455 egsWarning(
"EGS_RandomGenerator::createRNG: unknown RNG type %s\n",
468 int iaux = sequence/30081;
469 int jxx = sequence - iaux*30081;
472 if (iaux > 31328)
egsFatal(
"EGS_RandomGenerator::defaultRNG: "
473 "sequence %d is outside of allowed range\n",sequence);
Base random number generator class. All random number generators should be derived from this class.
virtual void fillArray(int n, EGS_Float *array)=0
Fill the array of n elements pointed to by array with random numbers.
EGS_RandomGenerator(int n=128)
Construct a RNG that has an array of n points to hold random numbers.
static EGS_RandomGenerator * defaultRNG(int sequence=0)
Returns a pointer to the default egspp 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.
int baseSize() const
The memory needed to store the base state.
virtual bool storePrivateState(ostream &data)=0
Store the state of the RNG to the output 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.
virtual void describeRNG() const
Describe this RNG.
virtual int rngSize() const =0
The memory needed to store the rng state.
int np
size of the array rarray
int ip
pointer to the next rarray element in the sequence
virtual bool setPrivateState(istream &data)=0
Set the state of the RNG object from the data in the input stream data.
virtual EGS_RandomGenerator * getCopy()=0
Get a copy of the RNG.
virtual void resetState()=0
Reset the state to a previously saved state.
EGS_I64 count
random number generated so far
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.
bool setPrivateState(istream &data)
bool storePrivateState(ostream &data)
void fillArray(int n, EGS_Float *array)
Fill the array pointed to by array with random numbers.
void describeRNG() const
Output information about this RNG using egsInformation()
Global egspp functions header file.
EGS_RandomGenerator class header file.
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,...
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
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,...
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.