Random number generator

The random number generator is pretty simple. A 16-bit seed is stored which for convenience in code is split into two 8-bit values. The routine is at 0xdd4 in the sf2ua ROM. Here’s the equivalent C code:

short sf2rand(void) {
    int x = (g.randSeed1 <> 8;
    g.randSeed2 += x;
    g.randSeed1  = x;
    return g.randSeed2;

As far as PRNGs go it’s pretty poor, but good enough for our purpose here. The output of it is often bitmasked to limit its values (e.g. masked with 0xf to get a number from 0-15, masked with 0xe to get an even number from 0-14, and so on), and also used as a bit index with the BTST instruction to get a 1-in-N chance.

For example, to get a 1-in-4 chance:

if( 0x88888888 & (1 << (sf2rand() & 0x1f)))

The RNG is seeded with exactly the same value every time the machine starts (there’s nothing else to seed it with, no RTC, nothing), so it always produces the same string of random numbers.

If you start the machine, wait for the player invite screen to start, insert a coin and play with Ryu, you will face Dhalsim on your first fight. If you insert your coin while the copyright warning is still printing, you’ll face Blanka instead.

One of these days I should analyse it to see when the seed returns to its initial value, i.e. when the sequence starts repeating.

EDIT: Make sure you check out DIVVS·IVLIVS’s comments below, who’s studied that output from the RNG in detail

Random number generator

5 thoughts on “Random number generator

  1. DIVVS·IVLIVS says:

    >One of these days I should analyse it to see when the seed returns to its initial value, i.e. when the sequence starts repeating.

    There are:
    86 seeds with period 1,
    44 seeds with period 2,
    245 seeds with period 245,
    491 seeds with period 491,
    509 seeds with period 509,
    797 seeds with period 797,
    8501 seeds with period 8501,
    11329 seeds with period 11329,
    43534 seeds with period 43534.

    By the way, what are the initial values for g.randSeed1 and g.randSeed2? 🙂

    Liked by 1 person

  2. DIVVS·IVLIVS says:

    Okay, the initial values for g.randSeed1 and g.randSeed2 are $01 and $C3, respectively (i.e. the number 451).

    The random number generator cycles through the following 43534 values: 451, 1480, 4569, 13582, 40877, […], 46161, 7277, 21954, and 451 again.

    Liked by 1 person

  3. Welcome back, sf2platinum! Just one comment that I forgot to mention: it seems that, in sf2 at least, during the demo mode another (additional) source of randomization is used for the selection two opponents. So far, I have been unable to pinpoint the specific subroutine used.


  4. gizaha says:

    You can check how the red hadoken is created. It it’s indeed random, then it must be generated at one chance in the pseudo-RNG. My theory is that it isn’t depend only in RNG, but it depends at more sources (like energy bar, score, player positions or something). I will also search the code today.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s