Depends on context when you do it. I had several similar things (but for random weapons spawning within a given list) and punishing a random player when a team refused to stop camping.
I would declare two arrays (set to the size of MAXPLAYERS) and then do stacking operations. Kinda like how bl4nk suggested...
Code:
#include <sourcemod>
//Returns highest int found
public StackQueryHigh(const Stack[],const End)
{
decl I,X;
for(I = 0, X = End;Stack[I] != End;I++)
{
if(Stack[I] > X)
{
X = Stack[I];
}
}
return X;
}
public StackClear(Stack[],const End)
{
decl I;
for(I = 0;Stack[I] != End;)
{
Stack[I] = -1;
I++;
}
return;
}
public StackCount(const Stack[],const End)
{
decl I;
for(I = 0;Stack[I] != End;)
{
I++;
}
return I;
}
public StackEnd(const Stack[],const End)
{
decl I;
for(I = 0;Stack[I] != End;)
{
I++;
}
return I;
}
public StackFind(const Stack[],const Item,const End)
{
decl I;
for(I = 0;Stack[I] != End && Stack[I] != Item;)
{
I++;
}
if(Stack[I] == Item)
{
return I;
}
return -1;
}
public StackAdd(Stack[],const Item,const End)
{
if(StackFind(Stack,Item,End) == -1)
{
Stack[StackEnd(Stack,End)] = Item;
return true;
}
return false;
}
public StackRemove(Stack[],const Item,const End)
{
new Find = StackFind(Stack,Item,End);
if(Find == -1)
{
return false;
}
new EndItem = (StackEnd(Stack,End)-1);
Stack[Find] = Stack[EndItem];
Stack[EndItem] = End;
return true;
}
Add them to the specific team stack when you want them to be selected, and remove them from it when they leave or change team. Get the stack count to return how many items and use that to set the max int of the random generator.
Once you generate a number, you deference the array to get the stored client.
Code:
new Target = Blue[RandomNumber];
If it returns 0, then you know it's a dud. I don't imagine you would want to use this, but I'm presenting it in-case it's of any use, and this has worked for me before.