SA-MP Forums Archive
3 Random - 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: 3 Random (/showthread.php?tid=340275)



3 Random - COD - 06.05.2012

How could i select 3 random players to be part of a mission/etc but i have no idea how to make 3 random player selection?


Re: 3 Random - [MG]Dimi - 06.05.2012

Something like this?
pawn Код:
new first,second,third;


stock ChooseFirst()
{
    first = random(MAX_PLAYERS);
    if(!IsPlayerConnected(first)) return ChooseFirst();
    return first;
}

stock ChooseSecond()
{
    second = random(MAX_PLAYERS);
    if(!IsPlayerConnected(second) || (second == first)) return ChooseSecond();
    return second;
}

stock ChooseThird()
{
    third = random(MAX_PLAYERS);
    if(!IsPlayerConnected(third) || (third == first) || (second || first)) return ChooseThird();
    return third;
}



Re: 3 Random - 2KY - 06.05.2012

Something along these lines would probably work.

pawn Код:
new
    connectedPlayers = 0;
   
    for( new u; u < MAX_PLAYERS; u ++ ) // Get the amount of online players..
    {
        if( IsPlayerConnected( playerid ) )
        {
            connectedPlayers++;
        }
    }

new
    firstPlayer = random( connectedPlayers ),
    secondPlayer = random( connectedPlayers),
    thirdPlayer = random( connectedPlayers );
   
    printf("%s was randomly selected as the first player!\n%s was selected as the second player!\n%s was selected as the third player!", firstPlayer, secondPlayer, thirdPlayer );



AW: 3 Random - Nero_3D - 06.05.2012

pawn Код:
stock GetRandomPlayer(bool: init = false) {
    static
        iCount,
        iPlayers[MAX_PLAYERS]
    ;
    if(init) {
        for((_: init) = iCount = 0; (_: init) != MAX_PLAYERS; ++init) {
            if(IsPlayerConnected(_: init)) {
                iPlayers[iCount++] = _: init;
            }
        }
    }
    if(iCount != 0) {
        new
            rand = random(iCount)
        ;
        (_: init) = iPlayers[rand];
        iPlayers[rand] = iPlayers[--iCount];
        return _: init;
    }
    return INVALID_PLAYER_ID;
}
pawn Код:
new
    rand1 = GetRandomPlayer(true),
    rand2 = GetRandomPlayer(),
    rand3 = GetRandomPlayer()
;
But If I am not mistaken that foreach already got an random function (if you use it)


Re: 3 Random - COD - 06.05.2012

Yes, but how would i create a function like

PickRandomPlayer(3); and my variables aside by it like Mission[random] = 1;.


Re: 3 Random - 2KY - 06.05.2012

Quote:
Originally Posted by COD
Посмотреть сообщение
Yes, but how would i create a function like

PickRandomPlayer(3); and my variables aside by it like Mission[random] = 1;.
With Nero's function, just use Mission[rand1] = 1; (player 1), ect.