![]() |
|
A random number function with Allegro? |
IronFist
Member #913
January 2001
|
In my game, I am using the Allegro timer. The problem is when I use the function rand(), it only randomizes the number every second. The timer is set up like this: |
Matthew Leverton
Supreme Loser
January 1999
![]() |
What exactly are you trying to accomplish? In order to use random numbers, first you must seed the generator with srand(). A common approach is to seed it with the number of seconds since midnight, or something like that. Then you can call rand() whenever you want, and you'll always get a random number. So basically: |
Daniel_C_McKinnon
Member #685
October 2000
|
I don't think, Mathew, that you should reply to comments so crude. (That did seem a little crude) Anyways, I don't believe that the Allegro Timer Functions interfear with the rand() function. The rand() function isn't truly random at all, it's actually a hyper-complex formula that tries to accomplish a different number every time the CPU goes through a cycle. So the random number, in theory, is always different every time you call it, no matter what you do. UNLESS, you call srand( same_number ) before every time you call it. |
IronFist
Member #913
January 2001
|
I figured out what I did wrong. I had it set up right, but I had the srand() function in a function other than main() and that was messing it up for some reason. |
Matthew Leverton
Supreme Loser
January 1999
![]() |
DCM: It's "Matthew" with two T's. |
Bruce Perry
Member #270
April 2000
|
Clarification: random() exhibits the same behaviour. rand() is portable, but isn't very random in the lowest bits. Don't assume it returns 31 bits either - I think it returns 15 bits on some OSs/compilers/whatever (MingW32?) -- |
DanielH
Member #934
January 2001
![]() |
#include <time.h> |
Louster
Member #469
June 2000
|
I may be wrong, but the only thing I can think of that could cause the effect of 'only producing a different number every second' is if you had used srand((unsigned)time(NULL)) and were attempting to run the program frequently; thus the seed would only be changing every second. |
|