Mail Archives: djgpp/1997/03/08/01:42:54
Shawn Betts wrote:
>
> I was working with rand() and random() and noticed that there aren't any
> randomize functions that I could find for DJGPP. I found srand() and
> srandom() but they require a random seed. I would like a randomized timer
> function like so many other compilers I have seen (QBASIC, TURBO PASCAL 6.0,
> TURBO C++ 3.0). Does anyone know if I've over looked something. If I haven't
> how do I make a randomizer function?
What Borland's randomize() function does is seed the RNG with the clock
value. In DJGPP this is not automatic; instead you get to choose what
value to use as seed. To get an effectively random seed, use this bit
of code:
#include <stdlib.h>
#include <time.h>
void randomize( void )
{
srandom( (int) time( NULL ) );
return;
}
To get a random number from 0 to X - 1, use:
n = random( ) % X;
Before everybody gets hysterical on me again, let me say in advance that
the GNU random() RNG, unlike rand(), is guaranteed to be random in the
lower bits, so you can use the mod operator safely.
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | mailto:fighteer AT cs DOT com |
| God's final message to His Creation: | http://www.cs.com/fighteer |
| "We apologize for the inconvenience."| Fight against proprietary |
| - Douglas Adams | software - support the FSF!|
---------------------------------------------------------------------
- Raw text -