delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/07/17/11:00:05

From: charles AT pentek DOT com (Charles Krug)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: random generator
Date: 17 Jul 2001 14:46:35 GMT
Organization: Concentric Internet Services
Lines: 45
Message-ID: <slrn9l8jub.dcu.charles@homer.pentek.com>
References: <591c995d39584286 DOT 39584286591c995d AT poczta DOT arena DOT pl>
NNTP-Posting-Host: 205.158.181.210
User-Agent: slrn/0.9.7.0 (SunOS)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019