Re: Random number generator (normal random source)

From: d. hall (dhall@OOI.NET)
Date: 07/10/98


// thus on Fri, 10 Jul 1998 13:15:55 -0400, James wrote:

> In my code I needed numbers generated on a normal distribution (ie the
> bell curve thing) and use floats for that purpose.  Anyway, here's the
> code.

Normal (aka Gaussian) random number generation reminds me...  herein is a
function is heavily bastardized from Numerical Recipes in C.

ranf() if not defined can be duplicated via

#define ranf()          ((double) random() / (double) RAND_MAX)

Of course you'll have to link with -lm (which includes the function log(),
which conflicts with circle's log() function).  You can use something like
this to roll a players height and weigh, or if you're feeling really
motivated to devise a "normalized" spell damage system.

int
Gauss (int m, double s)
{
   static int pass = 0;
   static double y2;
   double x1, x2, w, y1;

   if (pass) {
      y1 = y2;
   } else {
      do {
         x1 = 2.0 * ranf () - 1.0;
         x2 = 2.0 * ranf () - 1.0;
         w = x1 * x1 + x2 * x2;
      } while (w >= 1.0);

      w = sqrt (-2.0 * log (w) / w);;
      y1 = x1 * w;
      y2 = x2 * w;
   }
   pass = !pass;

   return ((int) (y1 * s + (double) m));
}

note: double's are used since in ye olde time, all machine floats were
coverted to doubles before use.

d.


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST