The Random Number Generator class. More...
#include <ocsRanNumGen.hh>
Public Member Functions | |
RanNumGen (string InFileName) | |
Class constructor. | |
~RanNumGen () | |
Class destructor. | |
void | SaveCurrentState (void) |
Saves the current state of the random number generator. | |
void | ResetRanNumGen (void) |
or that was saved by a call to RanNumGen::SaveCurrentState | |
float | GetRanNum (void) |
Returns the next random number in the sequence. Returns values between 0 and 1. | |
float | GetRanCosineBiased (double alpha, double beta, double *LikelihoodRatio) |
This method generates a biased sample for importance sampling. | |
float | GetLikelihoodRatioForRanCosineBiased (double alpha, double ValFunction) |
float | GetRanSignBiased (double alpha, double *LikelihoodRatio) |
float | GetRanCosineBiased (double alpha, double *LikelihoodRatio) |
float | GetGaussianDeviate (void) |
void | ReadRanSeqParameters (string InFileName) |
void | WriteRanSeqParameters (string OutFileName) |
Writes the current state of the RNG to a file. | |
Private Attributes | |
long | idum |
long | idumStart |
long | idum2 |
long | idum2Start |
long | iy |
long | iyStart |
long | iv [NTAB] |
long | ivStart [NTAB] |
string | ContinuationFileName |
The name of the file to which the current state of the RNG is written. | |
string | RestartFileName |
int | DebugLevel |
DebugLevel. | |
Static Private Attributes | |
static const long | IM1 = 2147483563 |
static const long | IM2 = 2147483399 |
static const double | AM = 1.0/2147483563 |
static const long | IMM1 = IM1 - 1 |
static const long | IA1 = 40014 |
static const long | IA2 = 40692 |
static const long | IQ1 = 53668 |
static const long | IQ2 = 52774 |
static const long | IR1 = 12211 |
static const long | IR2 = 3791 |
static const int | NTAB = 32 |
static const long | NDIV = (1+IMM1/NTAB) |
static const double | RNMX = 1.0 - 1.2e-7 |
The Random Number Generator class.
The RanNumGen class uses the Random Number Generator (RNG) ran2 from Numerical Recipes in C. A helpful discussion of this random number generator can be found in the book Numerical Recipes in C or Online Numerical Recipes. Additional discussion can be found in "Random Number Generation and Monte Carlo Methods" by James E. Gentle (Springer).
The class contains methods that read and write the state of the RNG from and to a file. When running multiple jobs this feature enables the user to either continue along the random sequence from where you left off at the end of the last job or restart the RNG at the point in the sequence where the previous job started. The RNG can also be restarted within a job.
The class also contains methods that are used in importance sampling.
RanNumGen::RanNumGen | ( | string | InFileName | ) |
Class constructor.
The constructor reads parameters from an input parameter file.
InFileName,: | Name of the input parameter file (string) |
It does the following:
The possible OperationModes are
References ContinuationFileName, DebugLevel, idum, idum2, iy, LogFile, LogFileSeparator(), LOWER_AND_UPPER, LOWER_ONLY, OPTIONAL_NO_WARNING, ReadInt(), ReadLongInt(), ReadRanSeqParameters(), RestartFileName, SaveCurrentState(), and WriteRanSeqParameters().
RanNumGen::~RanNumGen | ( | ) |
Class destructor.
Calls the method void WriteRanSeqParameters(string OutFileName) which writes the final state of the RNG to a file. Hence it should be explicitly called in application programs!
References ContinuationFileName, and WriteRanSeqParameters().
Referenced by ChiSquareReceiver::~ChiSquareReceiver().
float RanNumGen::GetGaussianDeviate | ( | void | ) |
Returns a random sample from a the 1-dimensaional Gaussian distribution with mean 0 and standard deviation 1.
This code was taken directly from Numerical Recipes in C. See http://www.nr.com/ for documention.
References GetRanNum(), log(), and sqrt().
Referenced by RamanAmp::AddNoise(), OptAmplifier::AmplifyHomogeneous(), OptAmplifier::AmplifyOptSignalScalar(), OptAmplifier::AmplifyOptSignalVector(), OptAmplifier::AmplifyOptSignalVectorWithGainProfile(), OptFiberLocalError::FiberAmplifierAddMonteCarloNoise(), RamanAmp::PropagateNoise(), RamanAmp::PropagateSignalandNoise(), and OptAmplifier::PropagateSignalandNoise().
float RanNumGen::GetLikelihoodRatioForRanCosineBiased | ( | double | alpha, | |
double | ValFunction | |||
) |
Referenced by OptFiber::GetHeuristicBalanceForSndOrderPDV_VariableAngleExp(), and GetRanCosineBiased().
float RanNumGen::GetRanCosineBiased | ( | double | alpha, | |
double | beta, | |||
double * | LikelihoodRatio | |||
) |
This method generates a biased sample for importance sampling.
References GetRanNum(), and log().
Referenced by OptFiber::GetFiberSampleImportSamplingExp(), OptFiber::GetFiberSampleImportSamplingFstSndOrderPDV_Exp(), OptFiber::GetFiberSampleImportSamplingFstSndOrderPDV_VariableAngleExp(), OptFiber::GetFiberSampleImportSamplingSndOrderPDV_Exp(), OptFiber::GetFiberSampleImportSamplingSndOrderPDV_VariableAngleExp(), OptFiber::GetHeuristicBalanceForSndOrderPDV_VariableAngleExp(), and ReduPolarizTransformer::GetImportSampPolarizTransformation().
float RanNumGen::GetRanCosineBiased | ( | double | alpha, | |
double * | LikelihoodRatio | |||
) |
The method returns a float which is the value of the biased sample.
alpha,: | The biasing parameter. | |
LikelihoodRatio,: | The value of the liklihood ratio of the sample that has been generated. |
This is the biasing function that we used in the Reduced Model Applications.
References GetLikelihoodRatioForRanCosineBiased(), GetRanNum(), and log().
float RanNumGen::GetRanNum | ( | void | ) |
Returns the next random number in the sequence. Returns values between 0 and 1.
This code was taken directly from Numerical Recipes in C, function ran2.
See http://www.nr.com/ for documention.
References AM, IA1, IA2, idum, idum2, IM1, IM2, IMM1, IQ1, IQ2, IR1, IR2, iv, iy, NDIV, NTAB, and RNMX.
Referenced by RamanAmp::AddNoise(), Photodetector::AddShotNoise(), Photodetector::AddShotNoiseOLD(), OptAmplifier::AmplifyHomogeneous(), OptAmplifier::AmplifyOptSignalScalar(), OptAmplifier::AmplifyOptSignalVector(), OptAmplifier::AmplifyOptSignalVectorWithGainProfile(), RamanAmp::GeneticAlgorithms(), RamanAmp::GeneticAlgorithmsVaryCW(), RamanAmp::GeneticAlgorithmsVictor(), ReduOptFiber::GetFiberSampleCompleteScatt(), OptFiber::GetFiberSampleCompleteScatt(), OptFiber::GetFiberSampleImportSamplingCosRx(), OptFiber::GetFiberSampleImportSamplingExp(), OptFiber::GetFiberSampleImportSamplingFstSndOrderPDV_Exp(), OptFiber::GetFiberSampleImportSamplingFstSndOrderPDV_VariableAngleExp(), OptFiber::GetFiberSampleImportSamplingSndOrderPDV_Exp(), OptFiber::GetFiberSampleImportSamplingSndOrderPDV_VariableAngleExp(), OptFiber::GetFiberSampleUnifRxRzRy(), OptFiber::GetFiberSampleUnifRyRzRy(), OptFiber::GetFiberSampleUnifRz(), GetGaussianDeviate(), OptFiber::GetHeuristicBalanceForSndOrderPDV_VariableAngleExp(), ReduPolarizTransformer::GetImportSampPolarizTransformation(), OptFiberLocalError::GetNewFiberRealization(), GetRanCosineBiased(), OptSignal::GetRandomJones(), ReduOptFiber::GetRandomStepLengths(), OptFiber::GetRandomStepLengths(), GetRanSignBiased(), RamanAmp::PropagateNoise(), RamanAmp::PropagateSignalandNoise(), OptAmplifier::PropagateSignalandNoise(), BitString::SetRandomString(), RamanAmp::ShootingGeneticAlgorithms(), RamanAmp::ShootingGeneticAlgorithmsVaryCW(), and RamanAmp::ShootingGeneticAlgorithmsVictor().
float RanNumGen::GetRanSignBiased | ( | double | alpha, | |
double * | LikelihoodRatio | |||
) |
References GetRanNum(), log(), and sgn().
void RanNumGen::ReadRanSeqParameters | ( | string | InFileName | ) |
Initializes the state of the RNG by reading the state parameters from a file
References idum, idum2, iv, iy, LogFile, NTAB, and WriteRanSeqParameters().
Referenced by RanNumGen().
void RanNumGen::ResetRanNumGen | ( | void | ) |
or that was saved by a call to RanNumGen::SaveCurrentState
Resets the RNG to the state it was in at the beginning of the job
References idum, idum2, idum2Start, idumStart, iv, ivStart, iy, iyStart, and NTAB.
Referenced by OptFiber::GetHeuristicBalanceForSndOrderPDV_VariableAngleExp().
void RanNumGen::SaveCurrentState | ( | void | ) |
Saves the current state of the random number generator.
Should be used in conjunction with RanNumGen::ResetRanNumGen
References idum, idum2, idum2Start, idumStart, iv, ivStart, iy, iyStart, and NTAB.
Referenced by RanNumGen().
void RanNumGen::WriteRanSeqParameters | ( | string | OutFileName | ) |
Writes the current state of the RNG to a file.
References idum, idum2, iv, iy, and NTAB.
Referenced by RanNumGen(), ReadRanSeqParameters(), and ~RanNumGen().
const double RanNumGen::AM = 1.0/2147483563 [static, private] |
Referenced by GetRanNum().
string RanNumGen::ContinuationFileName [private] |
The name of the file to which the current state of the RNG is written.
Referenced by RanNumGen(), and ~RanNumGen().
int RanNumGen::DebugLevel [private] |
const long RanNumGen::IA1 = 40014 [static, private] |
Referenced by GetRanNum().
const long RanNumGen::IA2 = 40692 [static, private] |
Referenced by GetRanNum().
long RanNumGen::idum [private] |
Referenced by GetRanNum(), RanNumGen(), ReadRanSeqParameters(), ResetRanNumGen(), SaveCurrentState(), and WriteRanSeqParameters().
long RanNumGen::idum2 [private] |
Referenced by GetRanNum(), RanNumGen(), ReadRanSeqParameters(), ResetRanNumGen(), SaveCurrentState(), and WriteRanSeqParameters().
long RanNumGen::idum2Start [private] |
Referenced by ResetRanNumGen(), and SaveCurrentState().
long RanNumGen::idumStart [private] |
Referenced by ResetRanNumGen(), and SaveCurrentState().
const long RanNumGen::IM1 = 2147483563 [static, private] |
Referenced by GetRanNum().
const long RanNumGen::IM2 = 2147483399 [static, private] |
Referenced by GetRanNum().
const long RanNumGen::IMM1 = IM1 - 1 [static, private] |
Referenced by GetRanNum().
const long RanNumGen::IQ1 = 53668 [static, private] |
Referenced by GetRanNum().
const long RanNumGen::IQ2 = 52774 [static, private] |
Referenced by GetRanNum().
const long RanNumGen::IR1 = 12211 [static, private] |
Referenced by GetRanNum().
const long RanNumGen::IR2 = 3791 [static, private] |
Referenced by GetRanNum().
long RanNumGen::iv[NTAB] [private] |
Referenced by GetRanNum(), ReadRanSeqParameters(), ResetRanNumGen(), SaveCurrentState(), and WriteRanSeqParameters().
long RanNumGen::ivStart[NTAB] [private] |
Referenced by ResetRanNumGen(), and SaveCurrentState().
long RanNumGen::iy [private] |
Referenced by GetRanNum(), RanNumGen(), ReadRanSeqParameters(), ResetRanNumGen(), SaveCurrentState(), and WriteRanSeqParameters().
long RanNumGen::iyStart [private] |
Referenced by ResetRanNumGen(), and SaveCurrentState().
const long RanNumGen::NDIV = (1+IMM1/NTAB) [static, private] |
Referenced by GetRanNum().
const int RanNumGen::NTAB = 32 [static, private] |
Referenced by GetRanNum(), ReadRanSeqParameters(), ResetRanNumGen(), SaveCurrentState(), and WriteRanSeqParameters().
string RanNumGen::RestartFileName [private] |
The name of the file to which the state of the RNG at the start of the current job is written.
Referenced by RanNumGen().
const double RanNumGen::RNMX = 1.0 - 1.2e-7 [static, private] |
Referenced by GetRanNum().