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-09-19 01:45:39 +00:00
|
|
|
u32 Math_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.
|
|
|
|
*/
|
|
|
|
void Math_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-09-19 01:45:39 +00:00
|
|
|
f32 Math_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
|
|
|
|
* manner in which Math_Rand_ZeroOne generates its result.
|
|
|
|
*/
|
2020-09-19 01:45:39 +00:00
|
|
|
f32 Math_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.
|
|
|
|
*/
|
|
|
|
void Math_Rand_Seed_Variable(u32* rndNum, u32 seed) {
|
|
|
|
*rndNum = seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the next pseudo-random integer from the provided rndNum.
|
|
|
|
*/
|
|
|
|
u32 Math_Rand_Next_Variable(u32* rndNum) {
|
|
|
|
return *rndNum = (*rndNum * 1664525) + 1013904223;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the next pseudo-random floating-point number between 0.0f and
|
|
|
|
* 1.0f from the provided rndNum.
|
|
|
|
*/
|
|
|
|
f32 Math_Rand_ZeroOne_Variable(u32* rndNum) {
|
|
|
|
u32 next;
|
|
|
|
|
|
|
|
next = (*rndNum * 1664525) + 1013904223;
|
|
|
|
// clang-format off
|
2020-05-15 22:53:58 +00:00
|
|
|
*rndNum = next; sRandFloat = (next >> 9) | 0x3F800000;
|
2020-05-15 17:58:14 +00:00
|
|
|
// clang-format on
|
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.
|
|
|
|
*/
|
|
|
|
f32 Math_Rand_Centered_Variable(u32* rndNum) {
|
|
|
|
u32 next;
|
|
|
|
|
|
|
|
next = (*rndNum * 1664525) + 1013904223;
|
|
|
|
// clang-format off
|
2020-05-15 22:53:58 +00:00
|
|
|
*rndNum = next; sRandFloat = (next >> 9) | 0x3F800000;
|
2020-05-15 17:58:14 +00:00
|
|
|
// clang-format on
|
2020-05-15 22:53:58 +00:00
|
|
|
return *((f32*)&sRandFloat) - 1.5f;
|
2020-05-15 17:58:14 +00:00
|
|
|
}
|