Added improved random number generator from SMLIB.

This commit is contained in:
Richard Helgeby 2011-06-17 14:44:51 +02:00
parent 778ebc65f3
commit 3776eca312
2 changed files with 25 additions and 1 deletions

View File

@ -203,7 +203,7 @@ InfectOnClientDisconnect(client)
} }
// Get a random valid array index. // Get a random valid array index.
new randindex = GetRandomInt(0, eligibleclients - 1); new randindex = Math_GetRandomInt(0, eligibleclients - 1);
// Get the client stored in the random array index. // Get the client stored in the random array index.
new randclient = GetArrayCell(arrayEligibleClients, randindex); new randclient = GetArrayCell(arrayEligibleClients, randindex);

View File

@ -412,3 +412,27 @@ ZRBoolToString(bool:value, String:output[], maxlen)
strcopy(output, maxlen, "0"); strcopy(output, maxlen, "0");
} }
} }
/**
* (from SMLIB 0.10.2)
*
* Returns a random, uniform Integer number in the specified (inclusive) range.
* This is safe to use multiple times in a function.
* The seed is set automatically for each plugin.
* Rewritten by MatthiasVance, thanks.
*
* @param min Min value used as lower border
* @param max Max value used as upper border
* @return Random Integer number between min and max
*/
#define SIZE_OF_INT 2147483647 // without 0
stock Math_GetRandomInt(min, max)
{
new random = GetURandomInt();
if (random == 0) {
random++;
}
return RoundToCeil(float(random) / (float(SIZE_OF_INT) / float(max - min + 1))) + min - 1;
}