Fixed randomness

* Fixed mistake that caused random values to follow a constant pattern
This commit is contained in:
RGreenlees 2024-05-28 23:13:38 +01:00 committed by pierow
parent 36318cd1d4
commit ef7e7a9178

View file

@ -403,6 +403,7 @@ Vector UTIL_RandomPointOnCircle(const Vector origin, const float radius)
{
Vector result;
srand(time(NULL));
float random = ((float)rand()) / (float)RAND_MAX;
float a = random * (MATH_PI * 2);
@ -642,6 +643,7 @@ float fInterpConstantTo(float start, float end, float DeltaTime, float InterpSpe
float frandrange(float MinValue, float MaxValue)
{
srand(time(NULL));
return ((float(rand()) / float(RAND_MAX)) * (MaxValue - MinValue)) + MinValue;
}
@ -649,11 +651,14 @@ int irandrange(int MinValue, int MaxValue)
{
if (MinValue == MaxValue) { return MinValue; }
srand(time(NULL));
return MinValue + rand() / (RAND_MAX / (MaxValue - MinValue + 1) + 1);
}
bool randbool()
{
srand(time(NULL));
return (rand() & 1);
}
@ -661,6 +666,8 @@ Vector random_unit_vector_within_cone(const Vector Direction, double cone_half_a
{
Vector Result = ZERO_VECTOR;
srand(time(NULL));
double u = ((double)rand() / RAND_MAX) * 2 - 1; // random number in [-1, 1]
double phi = ((double)rand() / RAND_MAX) * 2 * M_PI; // random number in [0, 2*pi]
double r = sqrt(1 - u * u);