Mail Archives: djgpp/2001/07/17/11:00:05
On Tue, 17 Jul 2001 10:45:56 +0200, Jan Kowalski <delorie AT poczta DOT arena DOT pl>
wrote:
> Hi !
>
> I have some problems with random generator. I use rand() function to
> generate random valua. When I start my program evrey time its generates
> the same value. What am I doing with it ? :(
>
The usual way to do this is srand(time(NULL)); which will seed the generator
wiht a number likely to be unique.
This is actually a Very Good Thing.
Suppose you're creating an AI that is supposed to react to "random" events . .
say the AI is aiming an artilery round, calculating elevation and bearing
given "random" wind, "random" movement, and "random" location of the target.
You want to compare three different implementations.
What you do is you give rand() the same seed for each of the three tests and
see which of the three gives better results. . . srand(17); frinstance
Then, to guard against the possibility that the particular seed was favorable
to a particular AI model, you do repeated runs using different seeds and
approach it statictically. Using the technique from before:
int randSeed = time(NULL);
srand(randSeed);
// First test goes here
srand(randSeed)
// Secont test . . .
etc.
This gives you the power . . power is good . . .to control whether you want to
use the same numbers each run OR different numbers, depending on the needs of
the application.
As I said, a Very Good Thing.
Charles
- Raw text -