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++ > Problem with rand() function (ndslib)

#40765 - slum - Fri Apr 22, 2005 8:49 am

Hi everyone.

I need to generate a random integer using rand() function.
I wrote this :

Code:
int gen=(int)(rand() & 0xFFFF);


but it doesn't work, generating the same number (~1400).
I think I need te get the ds timestamp, but I don't know how...

Thank you.

P.S. : Sorry, it's not in DS dev forum :/ please moive it :)

#40785 - tepples - Fri Apr 22, 2005 4:41 pm

Wait for a keypress first, and then take the time from startup to keypress and plug that into srand(). Notice that virtually every official TETRIS? game for a Nintendo console needs at least three keypresses between power-on and gameplay start, if only to generate seedage for the PRNG.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#40880 - slum - Sat Apr 23, 2005 9:05 am

ok, thank you, but how can I get time betwween start and keypress ?
I'm wondering if srand exists in nds librairies...

thank you

#40908 - tepples - Sat Apr 23, 2005 1:57 pm

slum wrote:
ok, thank you, but how can I get time betwween start and keypress ?

Count the vblanks for one. For more seedage per press, use a keypress interrupt to mix VCOUNT into the seed. (Based on the docs, it looks like the X and Y buttons can't cause an interrupt. Strange.)

Quote:
I'm wondering if srand exists in nds librairies...

It's in newlib.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#41028 - wintermute - Sun Apr 24, 2005 10:01 am

NDS has an RTC ...