Mail Archives: djgpp/1998/04/10/21:00:22
Jude DaShiell wrote:
>
> All I was trying to do was to use clock() to seed srandom() since
> I think I've maybe figured out a technique for improving random number
> generation for programmers using gcc at least.
> One thing I noticed was that re-seeding of random number generators
> happened very
> predictibly in code I've read.
> I asked myself would any measureable effect come out of randomizing
> re-seeding?
It looks like you are trying to reinvent the square wheel. Reseeding
the random number generator is not a good way to "randomize" its value;
in fact, you should specifically NOT do it! random() is more than
capable of generating suitably random sequences without any interference
once you seed it initially. Take a look at the source code sometime!
The following code is recommended for clean random sequences:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main( void )
{
int i;
srandom( (int) time( NULL ) );
for ( i = 0; i < 100; i++ )
printf( "%5d ", random( ) % 10000 );
return 0;
}
Try it. Of course, since time()'s granularity is 1 second, running the
program more often than that will generate an identical random sequence,
but since you're talking about a game, I hope that won't be a problem.
If I'm misinterpreting your intentions, please tell me and I'll try to
think of something better. :-)
--
---------------------------------------------------------------------
| John M. Aldrich | "It may be better to be a live jackal|
| aka Fighteer I | than a dead lion, but it is better |
| mailto:fighteer AT cs DOT com | still to be a live lion." |
| http://www.cs.com/fighteer | - Lazarus Long |
---------------------------------------------------------------------
- Raw text -