OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
Random Numbers

Generates and displays random 16-bit numbers on button press. Every game needs randomness — enemy spawns, item drops, damage variance, shuffle order. The SNES has no hardware RNG, so we use a software pseudo-random number generator (LFSR) seeded from the frame counter.

Screenshot

What You'll Learn

  • How to generate random numbers with rand() (returns 0-65535)
  • How to seed the PRNG with srand() for unpredictable sequences
  • Why seeding from getFrameCount() at a player-triggered moment gives good randomness
  • How to display numbers in both hex and decimal with the text module

Controls

Button Action
A Generate a new random number
B Re-seed the PRNG from the frame counter

SNES Concepts

Why Seed Matters

rand() produces a deterministic sequence — the same seed always gives the same numbers. For a game, you want unpredictability. The trick: call srand(getFrameCount()) when the player presses START on the title screen. Since the player presses at an unpredictable frame, the seed varies each playthrough.

Using rand() in Games

u16 enemy_x = rand() % 256; // Random X position (0-255)
u16 damage = 10 + (rand() % 6); // Damage 10-15
u8 drop = (rand() % 100) < 20; // 20% chance of item drop
static s16 enemy_x[4]
X positions of the four static enemy sprites.
Definition main.c:63
u16 rand(void)
Get random 16-bit number.
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47

Modules Used

Module Why it's here
console consoleInit(), WaitForVBlank(), NMI handler setup
sprite OAM buffer (required by NMI handler)
dma DMA transfers used internally by console init
background BG layer configuration
text textInit(), textPrintAt(), textFlush() for number display
input padPressed() for single-press button detection

Build & Run

cd $OPENSNES_HOME
make -C examples/basics/random

Open random.sfc in Mesen2. Press A repeatedly, then press B and press A again — the sequence changes.