SA-MP Forums Archive
Random playerid - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Random playerid (/showthread.php?tid=282597)



Random playerid - Improvement™ - 11.09.2011

Title says it all, I have been trying a few different methodes by making a normale stock to get a Random playerid, but it seems alot more complicated than it actually looks like.

pawn Код:
new rPlayerid = random(10);
This code choses a random number from 0 to 10.

But what I want is to get a random number from the connected players, heres an example:
List of 4 connected players.
Код:
0. Debby
2. Mike
3. Kyle
5. Jack
And my question is: How would it be possible to get a random number from 0, 2, 3 or 5?


Re: Random playerid - Sasino97 - 11.09.2011

Quote:
Originally Posted by Improvement™
Посмотреть сообщение
Title says it all, I have been trying a few different methodes by making a normale stock to get a Random playerid, but it seems alot more complicated than it actually looks like.

pawn Код:
new rPlayerid = random(10);
This code choses a random number from 0 to 10.

But what I want is to get a random number from the connected players, heres an example:
List of 4 connected players.
Код:
0. Debby
2. Mike
3. Kyle
5. Jack
How would it be possible to get a random number of 0, 2, 3 or 5?
You have to make a loop that while the player is not online, then select another random number.

pawn Код:
new rPlayerid = random(10);
while(!IsPlayerConnected(rPlayerid)) rPlayerid = random(10);
Be careful, if no player is connected, when this loop is started, It will never end


Re: Random playerid - Babul - 11.09.2011

[GF]Sasino97's method works faster if there are a lot of players connected, it picks one random number and checks if hes connected. however, it needs some more time if less players are online, hence your max_players, and how many are connected. the less the online-players-percentage is, the longer the algorithm will need.
a linear-time consuming algorithm will work, by not checking for non-connected player(s) several times.
Код:
CMD:players(playerid,params[])
{
	new MaxPlayers=GetMaxPlayers();
	new List[MaxPlayers];
	new PlayersFound;
	for(new pid=0;pid<MaxPlayers;pid++)
	{
		if(IsPlayerConnected(pid))
		{
			List[PlayersFound]=pid;
			PlayersFound++;
		}
	}
	new rnd=random(PlayersFound);
	new string[128];
	new Name[MAX_PLAYER_NAME];
	GetPlayerName(List[rnd],Name,sizeof(Name));
	format(string,sizeof(string),"[%d](%d)%s",rnd,List[rnd],Name);
	return 1;
}
my method will "waste" checking 499 player slots, if there is only 1/500 player online, but it uses the same CPU time with a full server ^^
i want to repeat: [GF]Sasino97's method will do the trick a LOT faster with a low set MAX_PLAYERS, or a full server
btw: if anyone wonders why i posted an answer despite its already solved: its "logarithmic time" vs "linear time", where the superiority of a linear time using algorithm should be obvious... as soon you want to print more than just 1 random player, you will see why...


Re: Random playerid - =WoR=Varth - 11.09.2011

Why don't you use foreach btw?
It have "Iter_random(Player);"


Re: Random playerid - admantis - 11.09.2011

Another very similar solution.
pawn Код:
stock RandomPlayer( )
{
    static int;
    for(new i = 00; i < MAX_PLAYERS; i++ )
    {
        if ( !IsPlayerConnected ( i ) ) continue;  
        int += i;
    }
    return random(int);
}
// usage:
rPlayer = RandomPlayer();



Re: Random playerid - [M]onsieur - 11.09.2011

admantis
Its function returns wrong values

Gassino
Its function crashes the server, in the case is empty

Finally:
pawn Код:
RandomP()
{
    new a = -1, b[MAX_PLAYERS];

    for(new i; i != MAX_PLAYERS; i++) if(IsPlayerConneted(i))
    {
        b[++a] = i;
    }
    return a == -1 ? -1 : b[random(a)];
}



Re: Random playerid - [L3th4l] - 11.09.2011

pawn Код:
stock GetRandomPlayer()
    return Iter_Random(Player);



Re: Random playerid - [M]onsieur - 11.09.2011

pawn Код:
#define GetRandomPlayer() Iter_Random(Player)



Re: Random playerid - admantis - 12.09.2011

Quote:
Originally Posted by [M]onsieur
Посмотреть сообщение
admantis
Its function returns wrong values

Gassino
Its function crashes the server, in the case is empty

Finally:
pawn Код:
RandomP()
{
    new a = -1, b[MAX_PLAYERS];

    for(new i; i != MAX_PLAYERS; i++) if(IsPlayerConneted(i))
    {
        b[++a] = i;
    }
    return a == -1 ? -1 : b[random(a)];
}
My bad.