Re: Random number calls

From: d. hall (dhall@APK.NET)
Date: 01/07/98


// thus on Wed, 7 Jan 1998 13:17:18 -0600, Brian virtually wrote:

Brian> This may seem like a stoopid question, but when a random number is
Brian> called for in the code (such as for hitpoint gains or offensive
Brian> spells), is that number truly random?  Or is there a way that a mort
Brian> could somehow know when to have the random number called if he/she
Brian> wanted a certain result?  I've heard something to this effect (that
Brian> the numbers aren't truly random) somewhere.. thanks.

There is no such thing as a truly random number.  All computer generated
numbers are pseudo-random, in that they will select a number from a certain
range over a certain period.  It depends upon which random numbers
generator you have, but they're normally perdictable and will repeat
themselves over a period of (worst case) 2^32.  Best case register-shift
algorithms can have a period of 2^249 (but they can also be very slow).

So unless you've got someone who wants to try >1 million tries at the same
thing, and even then there are often several people all using the same
random number generator, so it doesn't insure accurate results.  There are
some broken rand() functions which only alternate the lower-order bits,
which is why Jeremy included the random.c into circle.

So basically, unless your rand() function is seriously deficient, there
isn't any easy way to predict a random number in the game.  random() is
random enough for a mud.

If you want a uniformly random function over a specified range, then
you can replace the number() function in circle with the following.

/* some older machines convert all floating point to doubles */
#ifdef USE_HARDWARE_FLOAT
#define FLOAT           float
#else
#define FLOAT           double
#endif

/* quick and dirty implementation to make use of the high order bits */
#define ranf()          ((FLOAT) random() / (FLOAT) RAND_MAX)

int irand (int min, int max)
{
   if (min > max)
      return ((int) (ranf() * (FLOAT) (min - max + 1) + max));
   else
      return ((int) (ranf() * (FLOAT) (max - min + 1) + min));
}

#undef FLOAT
#undef ranf

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