delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/08/15:45:12

Message-ID: <343A8B8D.4EA07EA2@provider.com.br>
Date: Tue, 07 Oct 1997 16:20:45 -0300
From: Antonio Dias <accdias AT provider DOT com DOT br>
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Random numbers again...

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

- Raw text -


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