3 Random
#1

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?
Reply
#2

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;
}
Reply
#3

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 );
Reply
#4

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)
Reply
#5

Yes, but how would i create a function like

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

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)