Mail Archives: djgpp/1996/08/28/05:18:00
Mark Gilbert wrote:
>
> I can't figure rand() and random() out. They both seem positively un-random
> giving out the same numbers every time.
>
> Does anyone know of a problem with these functions in DJGPP?
> Does anyone know how to get them working?
>
Are you sure :
1/ that you include the <stdlib.h> header
2/ that before calling rand(), you *first* call the srand() function,
which initialize rand(), if you don't things like this *will* occur. Not
that everytime your call srand() with the same parameter, you get the
same random serie (a useful feature if you need to use the same random
numbers several times.
If this does not help, post the piece of code that calls rand() and an
example of the results you get...
Also, rand() has a bug, (but not the one you describe, at least on my
machine...) : it is not *very* random, in the sense that the numbers it
outputs "loop over" every about 30 million iterations. This is no big
deal if you justwant some "slight" alea, without caring whether this is
actually random or not. But if you are doing anything involving *real*
randomness, use random() or, better, a specific routines (places to look
for this are Numerical Recipes in C, 2nd edition, they have a web page
with the book online, look it up..., and Knuth's Art of Computer
Programming, part II).
Regards,
Francois
- Raw text -