15.5. How to Create Other Random Number Distributions

Suppose one wants to draw numbers according to a distribution that is not offered in Swarm. For example, the Beta distribution is a distribution that has two parameters. The Beta distribution can take on almost any unimodal shape, ranging from uniform, to highly skewed to the left or right (see Law and Kelton, p. 166). The Beta distribution can be produced by taking two draws from a particular Gamma distribution and transforming them. For example, one can first create the gammaDist1 and then put it to use:

- initializeRNGs
  [randomGenerator setStateFromSeed: 34733];
  gammaDist1= [GammaDist  create: self  setGenerator: randomGenerator setAlpha: 2 setBeta: 1];
  return self;

-(double) getBetaVariateAlpha1: (double) alpha1 Alpha2: (double) alpha2
   double y1, y2;
   y1=[gammaDist1 getSampleWithAlpha: alpha1  withBeta: 1];
   y2=[gammaDist1 getSampleWithAlpha: alpha2 withBeta: 1];
   return y1/(y1+y2);

With these methods defined, then the code must simply execute the initializeRNGs and then grab a Beta variate by specifying the two parameters, alpha1 and alpha2:

id gammaDist1;
double aDrawFromBeta;
[self initializeRNGs];
aDrawFromBeta = [self getBetaVariateAlpha1: .8  Alpha2: 2 ];