14.07.2011, 19:45
Here's my version. Returns a random player or INVALID_PLAYER_ID if no players are connected. Also its worth noting that foreach has an inbuilt random player function if you already use that library.
pawn Code:
GetRandomPlayer()
{
new
pCount,
rPlayers[MAX_PLAYERS] = {INVALID_PLAYER_ID, ...};
for(new playerid; playerid < MAX_PLAYERS; playerid++)
{
if(IsPlayerConnected(playerid))
{
rPlayers[pCount] = playerid;
pCount++;
}
}
if(pCount)
return rPlayers[random(pCount)];
else
return INVALID_PLAYER_ID;
}