2020-10-03 15:22:44 +00:00
|
|
|
#include "global.h"
|
2020-05-15 17:58:14 +00:00
|
|
|
|
|
|
|
// The latest generated random number, used to generate the next number in the sequence.
|
2020-05-15 22:53:58 +00:00
|
|
|
static u32 sRandInt = 1;
|
2020-05-15 17:58:14 +00:00
|
|
|
|
|
|
|
// Space to store a value to be re-interpreted as a float.
|
2020-05-15 22:53:58 +00:00
|
|
|
static u32 sRandFloat;
|
2020-05-15 17:58:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the next integer in the sequence of pseudo-random numbers.
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
u32 Rand_Next(void) {
|
2020-05-15 22:53:58 +00:00
|
|
|
return sRandInt = (sRandInt * 1664525) + 1013904223;
|
2020-05-15 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seeds the pseudo-random number generator by providing a starting value.
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
void Rand_Seed(u32 seed) {
|
2020-05-15 22:53:58 +00:00
|
|
|
sRandInt = seed;
|
2020-05-15 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-18 18:24:00 +00:00
|
|
|
* Returns a pseudo-random floating-point number between 0.0f and 1.0f, by generating
|
|
|
|
* the next integer and masking it to an IEEE-754 compliant floating-point number
|
2020-05-15 17:58:14 +00:00
|
|
|
* between 1.0f and 2.0f, returning the result subtract 1.0f.
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Rand_ZeroOne(void) {
|
2020-05-15 22:53:58 +00:00
|
|
|
sRandInt = (sRandInt * 1664525) + 1013904223;
|
|
|
|
sRandFloat = ((sRandInt >> 9) | 0x3F800000);
|
|
|
|
return *((f32*)&sRandFloat) - 1.0f;
|
2020-05-15 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a pseudo-random floating-point number between -0.5f and 0.5f by the same
|
2020-12-26 10:44:53 +00:00
|
|
|
* manner in which Rand_ZeroOne generates its result.
|
2020-05-15 17:58:14 +00:00
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Rand_Centered(void) {
|
2020-05-15 22:53:58 +00:00
|
|
|
sRandInt = (sRandInt * 1664525) + 1013904223;
|
|
|
|
sRandFloat = ((sRandInt >> 9) | 0x3F800000);
|
|
|
|
return *((f32*)&sRandFloat) - 1.5f;
|
2020-05-15 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seeds a pseudo-random number at rndNum with a provided seed.
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
void Rand_Seed_Variable(u32* rndNum, u32 seed) {
|
2020-05-15 17:58:14 +00:00
|
|
|
*rndNum = seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the next pseudo-random integer from the provided rndNum.
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
u32 Rand_Next_Variable(u32* rndNum) {
|
2020-05-15 17:58:14 +00:00
|
|
|
return *rndNum = (*rndNum * 1664525) + 1013904223;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the next pseudo-random floating-point number between 0.0f and
|
|
|
|
* 1.0f from the provided rndNum.
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Rand_ZeroOne_Variable(u32* rndNum) {
|
2021-02-14 00:49:40 +00:00
|
|
|
u32 next = (*rndNum * 1664525) + 1013904223;
|
2021-11-30 23:40:42 +00:00
|
|
|
|
2022-04-29 19:06:08 +00:00
|
|
|
sRandFloat = ((*rndNum = next) >> 9) | 0x3F800000;
|
2020-05-15 22:53:58 +00:00
|
|
|
return *((f32*)&sRandFloat) - 1.0f;
|
2020-05-15 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the next pseudo-random floating-point number between -0.5f and
|
|
|
|
* 0.5f from the provided rndNum.
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Rand_Centered_Variable(u32* rndNum) {
|
2021-02-14 00:49:40 +00:00
|
|
|
u32 next = (*rndNum * 1664525) + 1013904223;
|
2021-11-30 23:40:42 +00:00
|
|
|
|
2022-04-29 19:06:08 +00:00
|
|
|
*rndNum = next;
|
|
|
|
sRandFloat = (next >> 9) | 0x3F800000;
|
2020-05-15 22:53:58 +00:00
|
|
|
return *((f32*)&sRandFloat) - 1.5f;
|
2020-05-15 17:58:14 +00:00
|
|
|
}
|