diff --git a/include/random.h b/include/random.h index 85ff71abf..6bf61de6c 100644 --- a/include/random.h +++ b/include/random.h @@ -10,6 +10,9 @@ u16 Random2(void); //Returns a 32-bit pseudorandom number #define Random32() (Random() | (Random() << 16)) + +// The number 1103515245 comes from the example implementation of rand and srand +// in the ISO C standard. #define ISO_RANDOMIZE1(val)(1103515245 * (val) + 24691) #define ISO_RANDOMIZE2(val)(1103515245 * (val) + 12345) diff --git a/src/field_weather_effect.c b/src/field_weather_effect.c index 5a48f3434..26a7adf27 100644 --- a/src/field_weather_effect.c +++ b/src/field_weather_effect.c @@ -559,8 +559,7 @@ static void StartRainSpriteFall(struct Sprite *sprite) if (sprite->tRandom == 0) sprite->tRandom = 361; - // Standard RNG sequence. - rand = sprite->tRandom * 1103515245 + 12345; + rand = ISO_RANDOMIZE2(sprite->tRandom); sprite->tRandom = ((rand & 0x7FFF0000) >> 16) % 600; numFallingFrames = sRainSpriteFallingDurations[gWeatherPtr->isDownpour][0]; diff --git a/src/lottery_corner.c b/src/lottery_corner.c index bb3bb7a5c..5f68ad516 100644 --- a/src/lottery_corner.c +++ b/src/lottery_corner.c @@ -35,7 +35,7 @@ void SetRandomLotteryNumber(u16 i) u32 var = Random(); while (--i != 0xFFFF) - var = var * 1103515245 + 12345; + var = ISO_RANDOMIZE2(var); SetLotteryNumber(var); } diff --git a/src/pokemon_jump.c b/src/pokemon_jump.c index 11f88d1cf..6c76b15a6 100755 --- a/src/pokemon_jump.c +++ b/src/pokemon_jump.c @@ -1674,8 +1674,7 @@ static void sub_802BF7C(void) static int sub_802C098(void) { - // The number 1103515245 comes from the example implementation of rand and srand - gUnknown_02022CFC->unk24 = gUnknown_02022CFC->unk24 * 1103515245 + 24691; + gUnknown_02022CFC->unk24 = ISO_RANDOMIZE1(gUnknown_02022CFC->unk24); return gUnknown_02022CFC->unk24 >> 16; } diff --git a/src/random.c b/src/random.c index b570a7bc3..de923fba6 100644 --- a/src/random.c +++ b/src/random.c @@ -1,9 +1,6 @@ #include "global.h" #include "random.h" -// The number 1103515245 comes from the example implementation of rand and srand -// in the ISO C standard. - EWRAM_DATA static u8 sUnknown = 0; EWRAM_DATA static u32 sRandCount = 0; @@ -13,7 +10,7 @@ u32 gRng2Value; u16 Random(void) { - gRngValue = 1103515245 * gRngValue + 24691; + gRngValue = ISO_RANDOMIZE1(gRngValue); sRandCount++; return gRngValue >> 16; } @@ -31,6 +28,6 @@ void SeedRng2(u16 seed) u16 Random2(void) { - gRng2Value = 1103515245 * gRng2Value + 24691; + gRng2Value = ISO_RANDOMIZE1(gRng2Value); return gRng2Value >> 16; } diff --git a/src/script_pokemon_util_80F87D8.c b/src/script_pokemon_util_80F87D8.c index 243125448..134004fd0 100755 --- a/src/script_pokemon_util_80F87D8.c +++ b/src/script_pokemon_util_80F87D8.c @@ -434,7 +434,7 @@ void ScriptRandom(void) if (gLinkContestFlags & LINK_CONTEST_FLAG_IS_LINK) { - gContestRngValue = 1103515245 * gContestRngValue + 24691; + gContestRngValue = ISO_RANDOMIZE1(gContestRngValue); random = gContestRngValue >> 16; scriptPtr = &gSpecialVar_Result; } @@ -448,7 +448,7 @@ void ScriptRandom(void) u16 GetContestRand(void) { - gContestRngValue = 1103515245 * gContestRngValue + 24691; + gContestRngValue = ISO_RANDOMIZE1(gContestRngValue); return gContestRngValue >> 16; } diff --git a/src/time_events.c b/src/time_events.c index bd51f0c2d..3f56d3ab5 100644 --- a/src/time_events.c +++ b/src/time_events.c @@ -33,7 +33,7 @@ void UpdateMirageRnd(u16 days) s32 rnd = GetMirageRnd(); while (days) { - rnd = 1103515245 * rnd + 12345; + rnd = ISO_RANDOMIZE2(rnd); days--; } SetMirageRnd(rnd); diff --git a/src/water.c b/src/water.c index 760900702..1a66835fb 100644 --- a/src/water.c +++ b/src/water.c @@ -1762,7 +1762,7 @@ void sub_810871C(struct Task *task, u8 taskId) } task->data[11]++; task->data[8] = (task->data[8] + 39) & 0xFF; - task->data[7] = ((task->data[7] * 1103515245 + 12345) % task->data[5]) + task->data[4]; + task->data[7] = (ISO_RANDOMIZE2(task->data[7]) % task->data[5]) + task->data[4]; } void sub_81087C0(struct Sprite *sprite) diff --git a/src/wild_encounter.c b/src/wild_encounter.c index 52aac17f3..21f871751 100644 --- a/src/wild_encounter.c +++ b/src/wild_encounter.c @@ -127,12 +127,9 @@ static bool8 CheckFeebas(void) return FALSE; } -// The number 1103515245 comes from the example implementation of rand and srand -// in the ISO C standard. - static u16 FeebasRandom(void) { - sFeebasRngValue = (1103515245 * sFeebasRngValue) + 12345; + sFeebasRngValue = ISO_RANDOMIZE2(sFeebasRngValue); return sFeebasRngValue >> 16; }