Mail Archives: djgpp/1998/06/06/01:15:44
Dan Goodman wrote:
>
> >ANSI-standard RNG is rand(); however, DJGPP's implementation of it lacks
> >randomness in the lower bits. If portability is not an issue, we
>
> This explains a lot, have you any why this is? Should I just use random()
> instead?
I don't know the exact details of how DJGPP's rand() is non-random in
certain cases; I don't understand the underlying numerical theory well
enough. However, the DJGPP mailing list archives at
http://www.delorie.com/djgpp/mail-archives/ should allow you to look up
the discussions we've had in the past on the subject.
As for random(), as long as your program doesn't have to be portable, I
highly recommend using it instead of rand(). The only potential problem
is that random() takes more time to execute due to its complexity. (I'm
not sure how much longer; if you are really worried you should test it
yourself.) If you happen to have a PhD in numerical theory and want to
see exactly how random() works, feel free to download the library
sources (djlsr201.zip) and examine
src/libc/ansi/compat/stdlib/random.c. :-)
A final note: all pseudo-random number generators must be seeded before
being used, or they will always generate the same sequence of numbers.
The corresponding seed function for rand() is srand(); for random() it's
srandom(). Most programmers recommend using the system clock as a seed,
as in:
#include <stdlib.h>
#include <time.h>
srandom( (int) time( NULL ) );
hth!
--
---------------------------------------------------------------------
| John M. Aldrich | "A generation which ignores history |
| aka Fighteer I | has no past--and no future." |
| mailto:fighteer AT cs DOT com | |
| http://www.cs.com/fighteer | - Lazarus Long |
---------------------------------------------------------------------
- Raw text -