Replace do-while with for-loop in GetRandomValue rejection sampling

This commit is contained in:
Marcos De La Torre 2025-12-09 14:58:02 -08:00
parent 1a901da9a0
commit 216785b5af

View File

@ -1758,11 +1758,12 @@ int GetRandomValue(int min, int max)
unsigned long t = c - (c % m); // largest multiple of m <= c unsigned long t = c - (c % m); // largest multiple of m <= c
unsigned long r; unsigned long r;
do for (;;)
{ {
r = (unsigned long)rand(); r = (unsigned long)rand();
if (r < t) break; // Only accept values within the fair region
} }
while (r >= t);
value = min + (int)(r % m); value = min + (int)(r % m);
} }