i've used this for several commercial gba games. it's fast, simple, and
time-tested. mersenne twister-strength algorithms are generally not needed
for game-specific needs. no need to do the mode switch to ARM for so few
lines of assembly:
// C++ code
enum
{
RAND_SLOPE = 0x41c64e6d,
RAND_INTERCEPT = 0x3039,
RAND_SEED = 1,
};
static unsigned int sRandomPlant = RAND_SEED;
int rand()
{
sRandomPlant = (sRandomPlant * RAND_SLOPE) + RAND_INTERCEPT;
return static_cast<int>(sRandomPlant >> 17); // RAND_MAX = 0x7fff
}
// THUMB asm
RAND_SEED = 1
RAND_SLOPE = 0x3039
RAND_INTERCEPT = 0x41c64e6d
.data
randPlant:
.word RAND_SEED
.text
.thumb
.thumb_func
rand:
ldr r3, =randPlant
ldr r0, [r3]
ldr r1, intercept
ldr r2, slope
mul r0, r0, r2
add r0, r0, r1
str r0, [r3]
lsr r0, #17
bx lr
regards,
matt.
----- Original Message -----
From: "Graeme Cowie" <mcgeezer@...>
To: <gbadev@yahoogroups.com>
Sent: Tuesday, December 09, 2003 3:02 PM
Subject: [gbadev] Random number function
Hi gbadev forum,
Rather than re-invent the wheel trying to write an ARM random number
gernerator function, i thought i'd ask if anyone has done this before or if
there is an easy fast way to achieve random numbers.
I'm thinking of doing a GBA conversion of the classic Atari 8bit version of
River Raid, which generates terrain and enemies randomly.
Cheers,
McGeezer