slRandom really works?

Hi friends,

I'm in the middle of a nice project but have some problems with the function slRandom.

The game will be about cards, it's an Argentinian practice.

I have this code for mix the cards.

Previusly I have defined, MAX_CARDS 40, false 0 and true 1.






Code:
short mixCards()

{short index=0;

    for (i=0; i < MAX_CARDS; i++)

    {usado[i]=false;

      mazo_ordenado[i]=i;

    }

               

    for (i=0; i < MAX_CARDS; i++)

     {    

        do{

            //index = (rand() % 40 );

			testNum = slRandom() % MAX_CARDS;

			index = (short) testNum;

          }

        while (usado[index]==true);

     mazo_barajado[i] = mazo_ordenado[index];

     usado[index]=true;

     }

return true; 

}






The problem is the function slRandom does not change the seed, if I restart the console or emulator always show me the same values with the vector mazo_barajado[].

I cast the testNum to short, because, slRandom returns a FIXED value.

Any advice?
 
FacundoARG said:
Hi friends,

I'm in the middle of a nice project but have some problems with the function slRandom.

The game will be about cards, it's an Argentinian practice.

I have this code for mix the cards.

Previusly I have defined, MAX_CARDS 40, false 0 and true 1.






Code:
short mixCards()

{short index=0;

 for (i=0; i < MAX_CARDS; i++)

 {usado[i]=false;

 mazo_ordenado[i]=i;

 }

 for (i=0; i < MAX_CARDS; i++)

 { 

 do{

 //index = (rand() % 40 );

			testNum = slRandom() % MAX_CARDS;

			index = (short) testNum;

 }

 while (usado[index]==true);

 mazo_barajado[i] = mazo_ordenado[index];

 usado[index]=true;

 }

return true; 

}






The problem is the function slRandom does not change the seed, if I restart the console or emulator always show me the same values with the vector mazo_barajado[].

I cast the testNum to short, because, slRandom returns a FIXED value.

Any advice?




According to the SGL docs, slRandom() returns a FIXED type value ranging from 0.0 to 1.0 ...

That might be the problem, as I'm not sure the modulo would work the way you want with a FIXED number
wink.gif
 
FacundoARG said:
Hi friends,

I'm in the middle of a nice project but have some problems with the function slRandom.

The game will be about cards, it's an Argentinian practice.

I have this code for mix the cards.

Previusly I have defined, MAX_CARDS 40, false 0 and true 1.






Code:
short mixCards()

{short index=0;

    for (i=0; i < MAX_CARDS; i++)

    {usado[i]=false;

      mazo_ordenado[i]=i;

    }

               

    for (i=0; i < MAX_CARDS; i++)

     {    

        do{

            //index = (rand() % 40 );

			testNum = slRandom() % MAX_CARDS;

			index = (short) testNum;

          }

        while (usado[index]==true);

     mazo_barajado[i] = mazo_ordenado[index];

     usado[index]=true;

     }

return true; 

}






The problem is the function slRandom does not change the seed, if I restart the console or emulator always show me the same values with the vector mazo_barajado[].

I cast the testNum to short, because, slRandom returns a FIXED value.

Any advice?




I don't know about slRandom, but why not using the rand function instead ?

In order to init the rand seed, as you can't use srand(time(NULL)); as it is usually done on PC, you may need the following crappy code before your program main loop:

Code:
    SmpcDateTime *time = &(Smpc_Status->rtc);

    unsigned long seed;

    seed = slDec2Hex((Uint32)time->second)

         + 60*(slDec2Hex((Uint32)time->minute)

         + 60*(slDec2Hex((Uint32)time->hour)

         + 24*(slDec2Hex((Uint32)time->date)

         + 31*(time->month & 0x0f

         + 12* slDec2Hex((Uint32)time->year)))));

    srand(seed);

(This code has been adapted from SGM_device_backup.c in Rockin'B Save Game Manager, which was taken from sgl sample 13-1).

Then, in mixCards() function you can use the rand function:

Code:
    index = rand() % MAX_CARDS;

It seems to work under yabause, however I didin't tested it on real hardware.

I hope it will help your project.
 
Runik said:
According to the SGL docs, slRandom() returns a FIXED type value ranging from 0.0 to 1.0 ...

That might be the problem, as I'm not sure the modulo would work the way you want with a FIXED number
wink.gif

That is right, but If you call only one time slRandom() throws the same number even with FIXED numbers.

I think we can't change the seed of generic numbers.
sad.gif


Maybe if I get the time in seconds and then I call the function for example 59 times.

This one is not a great solution but it's something.
mellow.gif
 
Thanks cafe-alpha, I have already tested the rand() function also I have included the stdlib.h from the coff libraries.

But I will test it again with the srand() patched.
wink.gif
 
FacundoARG said:
Thanks cafe-alpha, I have already tested the rand() function also I have included the stdlib.h from the coff libraries.

But I will test it again with the srand() patched.
wink.gif

Thank for your fast reply
smile.gif


By the way, here is a sample project about the srand example : RandTest.zip.

In order to compile/test/etc, you need SaturnOrbit installed on your PC.

Just launch the "GO.BAT" file in randTest directory. Typing `r' in the command prompt will recompile the project, and typing `t' will test the program under yabause.

During the program execttion, hitting the Z key (mapped as keyboard's `D' under yabause) will call the rand function once.
 
slRandom stores its state in a system work area variable, maybe the idea is to manually set it to seed the RNG?
 
cafe-alpha said:
Thank for your fast reply
smile.gif


By the way, here is a sample project about the srand example : RandTest.zip.

In order to compile/test/etc, you need SaturnOrbit installed on your PC.

Just launch the "GO.BAT" file in randTest directory. Typing `r' in the command prompt will recompile the project, and typing `t' will test the program under yabause.

During the program execttion, hitting the Z key (mapped as keyboard's `D' under yabause) will call the rand function once.

Thanks friend, It works perfectly; now I need to test on a real Hardware. Your solution was to usefull
smile.gif


cafe-alpha said:
slRandom stores its state in a system work area variable, maybe the idea is to manually set it to seed the RNG?

Yes I knew it, it is the (Randwork), but I don't know how can I call it.
 
I use Mersenne twister random number generator, seeded with the system time if you are interested.
 
FacundoARG said:
Yes I knew it, it is the (Randwork), but I don't know how can I call it.

Going by the memory map section in the end of the SGL reference doc, the system work area is always located at 0x60ffc00, and Randwork is located at offset 0xb0. You'd probably have to disassemble the RNG to figure out if the location stores just an integer or a fixed-point value.

Amon said:
I use Mersenne twister random number generator, seeded with the system time if you are interested.

Isn't one property of the Mersenne twister algorithm that every time it has exhausted its state table it will generate new state data, causing varying execution time? I've always been a bit suspicious of using it in a real-time environment like games where strong randomness isn't a very high priority (but I've also never actually profiled the algorithm to see if the variation is significant.)
 
Back
Top