Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)
You are not logged in.
I'm trying to generate a random number in GBDK between 24 and 144 and I can't seem to get it to work.
My code looks like this: invader_x = rand() % (144 - 24 + 1) + 24; // move invader to random x position
and I have: #include <rand.h> at the top of my code
Any help woulfd be appreciated, thanks
Brian
Offline
And what is the problem? It does not compile or produce wrong values? Do you use GBDK-2020?
Offline
Wrong values, my sprite ends up off the left side of the screen sometimes.
Offline
Anybody?
Offline
Do you use GBDK-2020?
Offline
Yes, and I'm including rand.h
Offline
you must offset your sprite 8 pixels by X. top-left corner of visible screen for sprites is: X==8, Y==16
Offline
Is this the correct code to generate a random number between 24 and 140?
invader_x = rand() % (140 - 24 + 1) + 24;
Also is this all I need above in my code to set it up?
void initarand (uint8_t seed );
I'm still having trouble getting this to work
Offline
bkumanchik wrote:
invader_x = rand() % (140 - 24 + 1) + 24;
rand() returns a signed char which is -128..127. to make rand() return a positive value within 0..255 you must cast it to unsigned char:
r = ((unsigned char)rand() % (140 - 24 + 1)) + 24;
Offline
Thanks, I'll try that
Offline
That did it, Thanks!
Offline