#10361 - marcmccann - Wed Sep 03, 2003 1:57 pm
Hey guys, ive really only just started with GBA development but Ive found it easy to get to grips with so i didnt post in the beginners forum coz they probably wouldnt be able to answer my question.
I have tried to use the normal stdlib.h way of doing random which also incorporates time.h to get a true random number eg
Code: |
srand(time(NULL));
int SomeRandomNumber = rand() % 255;
|
however my DevKitAdv complains when I do this, is there any other way of getting random numbers into a GBA game without having to write my own macro or function to do it please?
In case you were wondering, I'm trying to do this to get random colours in my game menu's.
Any help would be appreciated on this guys, thanx! :)
_________________
The GBA is one Lean and Mean machine!
#10363 - Mr. GBA - Wed Sep 03, 2003 2:04 pm
Use these two functions:
#define RAND_MAX 32767
volatile s32 RAND_RandomData;
void SeedRandom(void)
{
RAND_RandomData = REG_VCOUNT;
}
s32 RAND(s32 Value)
{
RAND_RandomData *= 20077;
RAND_RandomData += 12345;
return ((((RAND_RandomData >> 16) & RAND_MAX) * Value) >> 15);
}
_________________
my dev/business site:
http://codebytesdev.afraid.org
#10364 - marcmccann - Wed Sep 03, 2003 2:08 pm
Thanx a lot Mr.Gba
_________________
The GBA is one Lean and Mean machine!
#10366 - Lupin - Wed Sep 03, 2003 2:19 pm
Mr GBA, why did you define the seed as volatile? Aren't variables always volatile?
Good idea to use the VCOUNT register to get an seed, I'd call the seedRandom function on every input interrupt, so you could be sure that the seed is really random
#10367 - Mr. GBA - Wed Sep 03, 2003 2:24 pm
Sorry, but I can't take credit for these functions. I got them from someone elses source code. BTW I'not sure why a volatile has been declared.
_________________
my dev/business site:
http://codebytesdev.afraid.org
#10369 - marcmccann - Wed Sep 03, 2003 2:28 pm
Thanx a lot anyway, I was about to try and write my own function which would have been messy! I dont quite understand the volatile either but as long as it works...
_________________
The GBA is one Lean and Mean machine!
#10400 - sgeos - Thu Sep 04, 2003 5:53 am
Lupin wrote: |
Aren't variables always volatile? |
Of course not. If all variables were always volatile, the keyword would be redundant. Volatile means that the something other than your code can change the value. Hardware, for example.
VCOUNT ought to be volatile if it is not. I did not look at the code for very long, but I do not see any reason for the state of an RNG to be declared volatile.
-Brendan Sechter
#10577 - marcmccann - Tue Sep 09, 2003 1:52 pm
From what I can gather, the variable is volatile because it is dependant on the hardware in order to get its value. If it wasnt volatile the chances of it actually working are VERY low and probably not worth trying, it works fine as it is and as long as it works i'll be using it like that...
_________________
The GBA is one Lean and Mean machine!