gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > random numbers

#2876 - eskimogd - Fri Feb 14, 2003 6:33 am

Hi i am new to GBA programming but have been doing windows programing for awhile. I have created a function that will generate psuedo random numbers but that isn't what i want. Every time i run the program i get the same pattern. I was wondering if there was anything on the GBA that i could seed my generator to so that it would create a true random. Something like seeding to time when you program for a computer. Thanks for your time.

-i am just glad there is a place like this where i could ask this question, thanks to the admins.

#2901 - gb_feedback - Fri Feb 14, 2003 10:37 am

There's been quite a lot of discussion of this on (I think) the yahoo gbadev group. Worth a search there.

But most schemes seem to be based on the length of time (eg how many frames) in takes a user to respond to something in the startup sequence before they press a key.

#2905 - Touchstone - Fri Feb 14, 2003 10:57 am

The contents of the gba memory isn't guaranteed at startup, maybe you could create a checksum and use it as rand seed? I think most gba-emulators clear the memory so even if this method works for the real deal it wouldn't work for emulators.

You could also respond to user-input, check when a button is pressed, how long it is being pressed etc.

If you for instance would want different boot-up or intro images (before user has pressed any keys) you could save a random seed in battery backup memory every time the game is started, if you have the possibility.
_________________
You can't beat our meat

#2943 - eskimogd - Fri Feb 14, 2003 9:15 pm

thanks guys im going to go check out the mailing list and see what i can find. I do like your idea though touchstone. I think the check sum is a good idea unfortunitly i don't have a linker yet. So if it doesn't work on an emu then im out of luck.

#2947 - beelzebub - Fri Feb 14, 2003 11:37 pm

The best thing to do is count the number vblanks until some user button press and use that to seed the random number generator, ie. the number of vblanks until START is pressed to exit the title screen.

#2949 - Daikath - Fri Feb 14, 2003 11:54 pm

I would use more variables since if a player is like me he hates the startup screen and at least tries to press start as fast as possible. I like the idea about putting a random number into a backupbattery save best :).

You could always change that number during gameplay since then there are a lot of different factors that can create a good number.

Though I wouldnt use just one method but several :).
_________________
?There are no stupid questions but there are a LOT of inquisitive idiots.?

#2950 - tepples - Sat Feb 15, 2003 12:33 am

If you want to get a good random seed even with an impatient player like Daikath who will almost always press start within 15 vblanks of power-on, I know of another technique that may work to generate about 16 bits of entropy:

1. Set up timer 3 to count using full range and 262144 Hz rate. It will count from 0 to 65535 and complete the cycle four times a second. Don't set the timer to go at maximum speed (16.78 MHz) because interrupt latency may ruin the entropy of the low-order bits.
2. Make sure all buttons are up.
3. Install an ISR that responds to keypress interrupts.
4. Display your title screen.
5. When you receive a keypress interrupt, copy the timer value to the seed value and then disable keypress interrupts.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#2956 - eskimogd - Sat Feb 15, 2003 2:41 am

wow thats a good idea tepples, thanks. I just threw in a simple counter that encrments every time my while statement passes to check for start. I have to learn a bit more about interupts. What is the best place that you have found that talks about using interupts with devkitadv. Thanks everyone that has answered.

#4264 - niltsair - Wed Mar 26, 2003 7:44 am

Timers aren't well implemented on Emulators though (i found it the hardway) On some it will generate an interrupt everytime it overflow, on others on each increase(if i remember right)