Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/RSPE01_01/symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21934,7 +21934,7 @@ lbl_804BF674 = .sbss:0x804BF674; // type:object size:0x4 data:4byte
lbl_804BF678 = .sbss:0x804BF678; // type:object size:0x4 data:4byte
lbl_804BF67C = .sbss:0x804BF67C; // type:object size:0x4 data:4byte
lbl_804BF680 = .sbss:0x804BF680; // type:object size:0x8 data:4byte
sSeed__11RPUtlRandom = .sbss:0x804BF688; // type:object size:0x4 align:4 data:4byte
sRandom__11RPUtlRandom = .sbss:0x804BF688; // type:object size:0x4 align:4 data:4byte
lbl_804BF690 = .sbss:0x804BF690; // type:object size:0x4 data:4byte
lbl_804BF694 = .sbss:0x804BF694; // type:object size:0x4 data:4byte
lbl_804BF698 = .sbss:0x804BF698; // type:object size:0x8 data:4byte
Expand Down
28 changes: 22 additions & 6 deletions include/Pack/RPUtility/RPUtlRandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ class RPUtlRandom {
* @brief Seeds the generator using the system clock
*/
static void initialize();
void initRand();

/**
* @brief Generates a random unsigned 32-bit integer
*/
static u32 getU32() {
return calc();
return sRandom.getRandU32();
}
u32 getRandU32() {
return calcRand();
}

/**
Expand All @@ -25,15 +29,21 @@ class RPUtlRandom {
* @param max Upper bound (exclusive)
*/
static u32 getU32(u32 max) {
return max * getF32();
return sRandom.getRandU32(max);
}
u32 getRandU32(u32 max) {
return max * getRandF32();
}

/**
* @brief Generates a random floating point value in the range [0, 1)
*/
static f32 getF32() {
return sRandom.getRandF32();
}
f32 getRandF32() {
// Limited to u16 bounds
u16 iRnd = static_cast<u16>(RANDF_MAX & (getU32() >> RANDF_SHIFT));
u16 iRnd = static_cast<u16>(RANDF_MAX & (getRandU32() >> RANDF_SHIFT));

// Convert to float
f32 fRnd = static_cast<f32>(iRnd);
Expand All @@ -47,7 +57,10 @@ class RPUtlRandom {
* @brief Advances the generator seed
*/
static u32 calc() {
return (sSeed = sSeed * MULT + 1);
return sRandom.calcRand();
}
u32 calcRand() {
return (mSeed = mSeed * MULT + 1);
}

private:
Expand All @@ -59,8 +72,11 @@ class RPUtlRandom {
//! Bit shift amount for random floating point generation
static const u32 RANDF_SHIFT;

//! Global generator seed
static u32 sSeed;
//! Global RNG
static RPUtlRandom sRandom;

//! Generator seed
u32 mSeed;
};

#endif
7 changes: 5 additions & 2 deletions src/Pack/RPUtility/RPUtlRandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <revolution/OS.h>

u32 RPUtlRandom::sSeed = 0;
RPUtlRandom RPUtlRandom::sRandom;
const u32 RPUtlRandom::RANDF_MAX = 0xFFFF;
const u32 RPUtlRandom::RANDF_SHIFT = 16;
const u32 RPUtlRandom::MULT = 0x10DCD;
Expand All @@ -11,12 +11,15 @@ const u32 RPUtlRandom::MULT = 0x10DCD;
* @brief Creates a seed based off of the system time.
*/
void RPUtlRandom::initialize() {
sRandom.initRand();
}
void RPUtlRandom::initRand() {
s64 ticks = OSGetTime();
OSCalendarTime calTime;

OSTicksToCalendarTime(ticks, &calTime);
//! Maybe there's a macro for this? Might look a bit fake-matchy at the
//! moment.
sSeed = (calTime.min << 26) | (calTime.sec << 20) | (calTime.msec << 10) |
mSeed = (calTime.min << 26) | (calTime.sec << 20) | (calTime.msec << 10) |
calTime.usec;
}
Loading