View Single Post
Re: Portable, self-contained replacements for RANDOM_STUFF()
Old
  (#22)
Whistler
Summoner
 
Whistler's Avatar
 
Status: Offline
Posts: 1,499
Join Date: Feb 2004
Location: Mist Village
Default Re: Portable, self-contained replacements for RANDOM_STUFF() - 14-04-2006

this is related with this thread, so instead opening a new one, just posting here.

well here is my implemention of random generator derived from an article in "Game Programming Gems", with a bit improvement, which is less "twisted" but random enough I guess:

PHP Code:
static long glSeed 0glGen2 0glGen1 0// our random number generator's seed

/**
 * This function initializes the random seed based on the initial seed value pass in the
 * initial_seed parameter.
 */
void lsrand(unsigned long initial_seed)
{
   
// Pick two large integers such that one is double the other
   
glGen2 3719;
   
glGen1 glGen2 2;

   
// fill in the initial seed of the random number generator
   
glSeed = (glGen1 initial_seed) + glGen2;
}

/**
 * This function is the equivalent of the rand() standard C library function, except that
 * whereas rand() works only with short integers (i.e. not above 32767), this f$
 * able to generate 32-bit random numbers. Isn't that nice?
 */
long lrand(void)
{
   if (
glSeed == 0// if the random seed isn't initialized...
      
lsrand(time(NULL)); // initialize it first
   
glSeed = (glGen1 glSeed) + glGen2// do some twisted math
   
return glSeed glSeed : -glSeed// and return absolute value of the result
}

/**
 * This function returns a random integer number between (and including) the starting and
 * ending values passed by parameters from and to.
 */
long RandomLong(long fromlong to)
{
   if (
to <= from)
      return 
from;

   return 
from lrand() / (LONG_MAX / (to from 1));
}

/**
 * This function returns a random floating-point number between (and including) the starting
 * and ending values passed by parameters from and to.
 */
long RandomLong(long fromlong to)
{
   if (
to <= from)
      return 
from;

   return 
from + (float)lrand() / (LONG_MAX / (to from));

btw, as long as the PDF spec. and free pdf viewers like xpdf are publically available (gv is also able to open pdf file), there is not problem to use the pdf format and it still sucks less than e.g., .doc format (except its DRM "features")...

Last edited by Whistler; 14-04-2006 at 10:24..
  
Reply With Quote