.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Bot Coding (http://forums.bots-united.com/forumdisplay.php?f=24)
-   -   Portable, self-contained replacements for RANDOM_STUFF() (http://forums.bots-united.com/showthread.php?t=244)

botmeister 08-01-2004 17:11

Re: Portable, self-contained replacements for RANDOM_STUFF()
 
You can use a seed derived from the system clock, that way each time your comupter app runs, it will use a different seed.

Whistler 14-04-2006 09:56

Re: Portable, self-contained replacements for RANDOM_STUFF()
 
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")... :)


All times are GMT +2. The time now is 16:22.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.