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.

DS development > Getting the System Time / Date

#55441 - GrenadeJumper - Wed Sep 28, 2005 3:07 pm

Hi All,

I'm trying to create a random number generator for a project I'm working on, but I can't generate a random seed to initialise it with.

So, my cunning plan is to use the system date and time! But I can't seem to find out how...

Here's my current code for the initialiser:
Code:
RandomInitialise(124,127);

(Note, the 124 and 127 are just numbers I put in there for it to work. Nothing important)
The only constraint on the numbers is that they have to be below 30,000.
I'm thinking, use something like:
Code:
Day x Month + ( Seconds + (hours x 3) + (minutes x 5) )
Seconds x Hours + ( Month + (Minutes x 3) + (Day x 5) )

Just for randomness :-)

Hope someone can help!

#55483 - cybereality - Wed Sep 28, 2005 9:05 pm

Code:
uint16 random ( uint16 range = 0 )
{
   static uint32 randSeed = 0xB0B15BAD;

   randSeed *= 0x0019660D;
   randSeed += 0x3C6EF35F;

   return ((randSeed >> 16) % range);
}

This function will return a pseudo-random number. Works good for most purposes. Just place the function at the beginning of the code and call it like this:

Code:
int rand = random(8);

Which will return a random integer between 0 and 7. That should be all you need. Hope thats what your looking for.
_________________
// cybereality

#55511 - GrenadeJumper - Wed Sep 28, 2005 11:16 pm

The problem with that code is that it generates the same number sequence every time i run it. That's the same problem i have with my hard-coded seed in the original generator.

In the libnds files, i just found the line
Code:
#define   REG_TIME      ( (vu16*)0x4000100)

Is that the system time? (*hopes*)

#55512 - josath - Wed Sep 28, 2005 11:25 pm

the time / date is all stored in the IPC. look at the fields in the struct in ipc.h. it's already all split into year/day/minute/second for you

#55513 - GrenadeJumper - Wed Sep 28, 2005 11:55 pm

Yay, thanks a ton!
:-D

#55514 - tepples - Wed Sep 28, 2005 11:55 pm

cybereality wrote:
Code:
uint16 random ( uint16 range = 0 )
{
   static uint32 randSeed = 0xB0B15BAD;

   randSeed *= 0x0019660D;
   randSeed += 0x3C6EF35F;

   return ((randSeed >> 16) % range);
}

It'll divide by zero if you use the default value for range. In this case you might as well not give a default value for range.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#55556 - GrenadeJumper - Thu Sep 29, 2005 10:31 am

Argh, a new problem in the same area!

Ok, this is the code:
Code:
   uint16 d = -(~IPC->curtime[3]) - 1;
   uint16 m = -(~IPC->curtime[2]) - 1;
   uint16 y = -(~IPC->curtime[4]) + 1;
   uint16 h = -(~IPC->curtime[5]) - 1;
   uint16 mi = -(~IPC->curtime[6]) - 1;
   uint16 s = -(~IPC->curtime[7]) - 1;

   if(h > 50) h-= 40;

   uint32 rand1 = d + m; // + s + h * 3 + mi * 5 + s;
   uint32 rand2 = s + h; // + m + mi * 3 + d * 5 + s;
   iprintf("\n\t\t%d, %d", rand1, rand2);
   RandomInitialise(rand1,rand2);


The values of 'd', 'm', 'y', 'h', 'mi' and 's' work, because they are printed later in the code. But, for some totally unknown, reason 'rand1' and 'rand2' are always 0 when they are printed, and the random numbers created after the 'RandomInitialise' routine are always the same.

Help!?!

#55568 - tuLL - Thu Sep 29, 2005 2:19 pm

I know nothing about programming.

But the Darkstar by Darkain has a list with all the system's settings in hex.

I don't know if it is open-source, but check it out.

#55592 - GrenadeJumper - Thu Sep 29, 2005 8:18 pm

The strange thing is, it worked for a while. I had it output the date + time to the console + it worked perfectly. But as soon as I tried to use it for the Random Number Generator seed, it failed! Argh!

Does anyone know why this would happen?

#55603 - Dwedit - Thu Sep 29, 2005 10:24 pm

There's always the method that systems used before they had internal clocks: Generating a new random number many times a second. Or constantly generate numbers while the joystick buttons are being pressed, or other input from the user to randomize.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."