Skip to content
Giovanni Bajo edited this page Jan 5, 2025 · 10 revisions

This short guide explains you how to obtain random numbers useful for random game logic / AI.

Use C rand()

As a starting point, C's rand() generates a number between 0 and RAND_MAX which on N64 is 2^31-1 (0x7fffffff). The sequence generated by rand() is deterministic, that is, it is always the same. To randomize the whole sequence, you can use srand(). This is how you would do:

// Put this code in `main()`, during the initialization phase.

// Option 1: just use the current CPU tick counter. Since this is reset at boot, this value will be similar across boots, but still not identical, so it might be sufficient.
srand(get_ticks());

// Option 2: use libdragon entropy pool, which uses real hardware entropy. 
srand(getentropy32());

// FORBIDDEN: do not use time(NULL). Nintendo64 doesn't normally have a RTC, so this will return -1, so a fixed seed
srand(time(NULL));  // DON'T DO THIS

After this call, rand() will return a new random number sequence, that is different after each boot.

Make it random on emulators too

The above will work as expected on real hardware, but not on emulators. The reasons are multiple and in general we could attribute this to imperfect emulation, but emulator authors will in general value consistency and reproducibility.

What we can do on emulators is instead trying to extract some randomness from user interaction. That is, we need to rely on the fact that the user will not press the start button (for instance) at exactly the same time; this difference in user interaction can be easily exploited by simply calling rand() every frame, with the expectation that when the game will truly need a random number (eg: to decide which enemy to spawn), a non deterministic number of frames will have been drawn.

// Put this code in `main()`, during the initialization phase, in addition to the srand call

// Call rand() at each vertical blank, ignore the result.
register_VI_handler((void(*)(void))rand);
Clone this wiki locally