Rejection loop in C



I understand what the below codes doin except that m_MaxDensityVal, DistributionDensity, are they pre-defined functions? is there any formula for them cause I need to define them in Pascal. Any help would be apprecited.
-----------

The sampling range of the needed distribution is [a,b] and the probability
density function (pdf) is denoted by f(x).
x1 = a + r1 * (b -a );
if( r2 * Maximum_of_PDF <= f( x1 ) )
then accept x1
else
throw away r1 & r2 and start again.

In principle a random number is accepted more often if the PDF for this value is bigger. Thus the desired distribution is sampled.
This is the so called rejection loop
Here is some code to illustrate the algorithm:

const double PI = 4.0 * atan(1.0);
/////////////////////////////////////////////////////////

double CRejectionMethod::DistributionDensity( double x )

// The implementation of the distribution density function.
// In this case its a normal distribution!
/////////////////////////////////////////////////////////{
double RetVal;

RetVal = 1 / ( sqrt( 2 * PI ) ) * exp( - (x * x) / 2. );
return RetVal;
}
/////////////////////////////////////////////////////////
double CRejectionMethod::GetRandomNr( double& a, double& b )
// This is the main routine, that implements the specific
// algorithm to deliver the random numbers between [a,b]
/////////////////////////////////////////////////////////{
double R1, R2;
double x;
int bReject = true;

m_MaxDensityVal = FindMaxDensityVal(); // for a normal distribution
this would be m_MaxDensityVal = DistributionDensity( 0.0 );

// main algorithm, the rejection loop
while( bReject == true ) {
R1 = GetlRandomNr();
x = a + (b - a) * R1;
R2 = GetlRandomNr();

if( (R2 * m_MaxDensityVal) <= (DistributionDensity(x)) )
return x;
}
return false;
}
.



Relevant Pages

  • Re: Big Push for Guest Worker Program
    ... so that I wouldn't every have to do taxes again. ... Since my big distribution comes in November, ... I remember my car license plate ID by an algorithm rather ...
    (soc.retirement)
  • Re: GetHashCode for Objects that Compare Based on Value (Not reference equality)
    ... Further, for those sets of data, an XOR hash produces a sufficiently ... random distribution for hashes to work just fine. ... realm of developing a good generic algorithm; ... Saying that XOR produces collisions is not very useful. ...
    (microsoft.public.dotnet.framework)
  • Re: GetHashCode for Objects that Compare Based on Value (Not reference equality)
    ... one cannot characterize 'all data' or even 'real life ... my algorithm you quickly get a broader range. ... the definition of random distribution, and thus should data in member ... It follows that given a class with random member variable values, ...
    (microsoft.public.dotnet.framework)
  • Re: Locating Nearest Neighbors in space (fast)
    ... The brute force algorithm that you described is O, ... so the nearest neighbour distribution is very wide. ... If your distribution of nearest neighbour separations is heterogeneous (like ... stars) then I'd recommend an octree or k-D tree (adaptively subdividing ...
    (comp.programming)
  • Re: Prime numbers
    ... The prime numbers are produced by the algorithm of the Eratosthenes ... That algorithm is equivalent to a Pseudorandom Generator. ... distribution known as the Gauss Distribution. ... "Une des hypotheses les plus interesantes auxquelles conduisent les ...
    (sci.math)