private double randomGaussianMeanMax(double mean, double max)
{
// DWS: I verified the results empirically
Debug.Assert(mean <= max && mean >= 0);
double g = randomGaussian();
double mean2 = mean;
double flip = 1;
if (g < 0)
{
mean2 = max - mean;
flip = -1;
g *= -1;
}
// pivot is the distance from mean2 towards max where the boundary of
// 1 standard deviation alters the calculation
double pivotMax = max - mean2;
double pivot = Math.Min(mean2, pivotMax / 2);//from 0 to max-mean2
Debug.Assert(pivot >= 0 && pivotMax >= pivot && g >= 0);
double pivotResult;
if (g <= 1)
pivotResult = pivot * g;
else
pivotResult = Math.Min(pivotMax, (g - 1) * (pivotMax - pivot) + pivot);
return mean + flip * pivotResult;
}