Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A random number function with Allegro?

This thread is locked; no one can reply to it. rss feed Print
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:
install_int_ex(increment_speed_counter, MSEC_TO_TIMER(25));
So it should be changing every millisecond, shouldn't it?
Do any of you know how to make the rand() function change like every millisecond instead of every second? Or do you know a better way to get random numbers with Allegro and the Allegro timer?

Matthew Leverton
Supreme Loser
January 1999
avatar

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:
srand(some_number);
x = rand() % 10; // (random number from 0-9)
You only have to call srand() once per game. You can also use srandom()/random(), but I forget what the differences between them are. I hope I haven't misunderstood your question though...
--
Matthew Leverton - matthew.leverton@interclay.com

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
avatar

DCM: It's "Matthew" with two T's. :)
I'm usually short to the point, and at 1:oo AM, I'll sound more crude than normal. In fact this message is kind of crude at 1o:oo AM, but probably because I haven't woken up yet.
In any case, at least he figured out his problem, so I won't have to give him any more crude answers. :)
--
Matthew Leverton - matthew.leverton@interclay.com

Bruce Perry
Member #270
April 2000

Clarification:
Call srand(). Choose any number you like.
Now print out the first ten values returned by rand().
Now call srand() again, with the same seed (that's the number by the way). Now print out the first ten values return by rand().
Notice a pattern?
rand() will always return the same sequence of numbers, given the same seed - it has NOTHING to do with the timer, or with CPU cycles.

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?)
random() isn't portable, but it returns better random numbers.
PS I don't think Matthew sounded crude - I must admit I too was confused by the assumption that rand() is in some way linked with the timer. Matthew did the right thing by explaining how rand() works from the basics (and the 'basically's :-) Anyway, I'm glad the problem's resolved.

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

DanielH
Member #934
January 2001
avatar

#include <time.h>
// somewhere probably in main
srand((unsigned)time(NULL));
int Random(int u)
{
// value 0-u
return(rand() % u);
}
int Random(int u,int t)
{
// value u-t
return Random(t-u+1)+u;
}
Don't know how portable that is.
1am? Thats when I do most of my programming mathew.
Oops sorry matthew :]

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.
Just attempting to clear it up, seeing as no-one else suggested this :P

Go to: