delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/23/12:01:46

From: amit <abarak AT iil DOT intel DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random numbers again...
Date: 23 Oct 1997 07:02:55 GMT
Organization: Intel Israel (74) Ltd.
Lines: 51
Message-ID: <62msqv$rc0$6@195.26.68.19>
References: <343A8B8D DOT 4EA07EA2 AT provider DOT com DOT br>
NNTP-Posting-Host: 195.26.68.19
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 <stdio.h>              // for printf()
> #include <stdlib.h>             // for random() and sramdom()
> #include <dos.h>                // 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 <accdias AT provider DOT com DOT br>
> 
> --- 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.

- Raw text -


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