From: amit Newsgroups: comp.os.msdos.djgpp Subject: Re: Random numbers again... Date: Wed, 15 Oct 1997 08:39:12 +0300 Organization: Intel Israel (74) Ltd. Lines: 50 Message-ID: <3444650F.C84@iil.intel.com> References: <343A8B8D DOT 4EA07EA2 AT provider DOT com DOT br> NNTP-Posting-Host: abarak-desk.iil.intel.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Antonio Dias wrote: > > Hi all, > > I was looking all that messages about random numbers and I decided to > investigate. I discover that to generate a true random number you must > use ramdom() function instead of rand(). These functions need to be > initialized with a seed to work well. This is acomplished using > srandom() function. The srandom() function expects a int as seed and if > your program always send the same seed it will *always* get the same > ramdom numbers. The problem now consists in pass a new seed to srandom > everytime you start your program. I acomplish this using the hundreds of > seconds from machine's internal clock. Look the code below: > > /******************** > * > * Generating ramdom numbers. > * > */ > #include // for printf() > #include // for random() and sramdom() > #include // for gettime() > > int main(void) { > int r; > struct time t; > > gettime(&t); > > srandom((int) t.ti_hund); > > for(int i = 1; i < 10; i++) { > r = random(); > printf("Random number -> %i\n"); > } > } > /* eof */ > > I'm very new in c/c++ programming, so excuse-me about any mistake. Sorry > about my bad english too! > > Huge Hugs, > Antonio Dias > > --- BIOS problem: Begginner Insane Operating System one comment : using only hundreds of seconds as seed in many cases might not be good enough - if you run your program more than 100 times you'r bound to have (at least) two identical runs.