Mail Archives: djgpp/1997/10/08/15:45:12
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 -