SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 04/06/2019, 06:47 AM   #1
LowIQ
Little Clucker
 
Join Date: May 2019
Posts: 21
Reputation: 5
Question Random (Choose Player On Event)

What is the best and most efficient way to get ONE random winner on event?

Code:
for(new i = 0; i < MAX_PLAYERS; i++)
{       
     if(IsPlayerOnEvent(i) == true)
     {
          //Need: RANDOM SIZEOF i (Choose One Winner From i)
          //GivePlayerMoney(WinnerID, 666);
     }

}
NOTE: If there is only ONE player on event it will choose him with 100% chance?

Thanks for replies !
PS: Be creative
LowIQ is offline   Reply With Quote
Old 04/06/2019, 11:17 AM   #2
Plastikmensch
Huge Clucker
 
Join Date: Aug 2016
Posts: 273
Reputation: 22
Default Re: Random (Choose Player On Event)

I would do it like this: get players on event, store their ids and select one of them:
Code:
new index;
for(new i=0,j=GetPlayerPoolSize();i<j;i++)
{
   if(IsPlayerOnEvent(i))
   {
      PlayersInEvent[index]=i;
      index++;
   } 
}
new WinnerID=PlayersInEvent[random(sizeof(PlayersInEvent))]
//Be careful. Infinite loop if no one is in event.
while(WinnerID==INVALID_PLAYER_ID) WinnerID=PlayersInEvent[random(sizeof(PlayersInEvent))];
GivePlayerMoney(WinnerID,...);
//You need to assign and reset PlayersInEvent with INVALID_PLAYER_ID or playerid 0 will always win
Or you just get the highest id on the server, and use that as max value.
Code:
new winnerID = random(GetPlayerPoolsize())
//pretty much same as above
There are probably way better ways to do that, but I worked with what you provided.
Plastikmensch is offline   Reply With Quote
Old 04/06/2019, 12:39 PM   #3
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,121
Reputation: 68
Default Re: Random (Choose Player On Event)

this can be done with many separate ways, but my favorite is to try avoid loop.
PHP Code:
stock GetRandomWinner()
{
    new 
rand random(GetPlayerPoolSize());
    if(
IsPlayerConnected(rand))
    {
        return 
rand;
    }
    else
    {
        while (
IsPlayerConnected(rand))
        {
            
rand random(GetPlayerPoolSize());
        }
    }
    return 
rand ;

__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Old 04/06/2019, 02:16 PM   #4
TheToretto
Gangsta
 
TheToretto's Avatar
 
Join Date: Jul 2015
Posts: 587
Reputation: 66
Default Re: Random (Choose Player On Event)

@Mugala:

pawn Code:
while (IsPlayerConnected(rand))
should be
pawn Code:
while (!IsPlayerConnected(rand))
otherwise it would be infinite or return a false ID.
__________________






Toretto#9902

Rest in peace grandpa, 03 November 2018
TheToretto is offline   Reply With Quote
Old 05/06/2019, 08:05 AM   #5
LowIQ
Little Clucker
 
Join Date: May 2019
Posts: 21
Reputation: 5
Default Re: Random (Choose Player On Event)

Quote:
Originally Posted by Mugala View Post
this can be done with many separate ways, but my favorite is to try avoid loop.
PHP Code:
stock GetRandomWinner()
{
    new 
rand random(GetPlayerPoolSize());
    if(
IsPlayerConnected(rand))
    {
        return 
rand;
    }
    else
    {
        while (
IsPlayerConnected(rand))
        {
            
rand random(GetPlayerPoolSize());
        }
    }
    return 
rand ;

I would like to do it with this option. But i dont really get how to do it right way...

So, i check all players if they are on event or not:

Code:
for(new i = 0; i < MAX_PLAYERS; i++)
{
     if(ONEVENT[i] == true)
     {
          new EWINNER;
          EWINNER = GetRandomWinner();
          GivePlayerMoney(EWINNER, 666);
      }

}

stock GetRandomWinner()
{
    new rand = random(GetPlayerPoolSize());
    if(IsPlayerConnected(rand) && ONEVENT[rand] == true)
    {
        return rand;
    }
    else
    {
        while (!IsPlayerConnected(rand))
        {
            rand = random(GetPlayerPoolSize());
        }
    }
    return rand ;
}
Is this right? No bug? Does it really choose one winner?

Last edited by LowIQ; 05/06/2019 at 08:36 AM.
LowIQ is offline   Reply With Quote
Old 05/06/2019, 09:05 AM   #6
SiaReyes
Huge Clucker
 
SiaReyes's Avatar
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 323
Reputation: 22
Default Re: Random (Choose Player On Event)

pawn Code:
stock GetRandomWinner()
{
    new rand = random(GetPlayerPoolSize());
   
    if(IsPlayerConnected(rand) && ONEVENT[rand])
    {
        return rand;
    }
    else
    {
        while (!IsPlayerConnected(rand))
        {
             if(ONEVENT[rand]) rand = random(GetPlayerPoolSize());
        }
    }
    return rand ;
}
__________________
Offshowtime Fuck It!

Quote:
Originally Posted by Toroi View Post
blackmail people that join your server using their ip address as hostage

or get a job and play sa-mp for fun
SiaReyes is offline   Reply With Quote
Old 05/06/2019, 11:10 AM   #7
Dev69
Little Clucker
 
Join Date: Jun 2019
Posts: 12
Reputation: 0
Default Re: Random (Choose Player On Event)

and if the player is not connected, just use the ++ to switch the winner to next playerid..
Dev69 is offline   Reply With Quote
Old 05/06/2019, 11:36 AM   #8
LowIQ
Little Clucker
 
Join Date: May 2019
Posts: 21
Reputation: 5
Default Re: Random (Choose Player On Event)

Quote:
Originally Posted by SiaReyes View Post
pawn Code:
stock GetRandomWinner()
{
    new rand = random(GetPlayerPoolSize());
   
    if(IsPlayerConnected(rand) && ONEVENT[rand])
    {
        return rand;
    }
    else
    {
        while (!IsPlayerConnected(rand))
        {
             if(ONEVENT[rand]) rand = random(GetPlayerPoolSize());
        }
    }
    return rand ;
}
OK THANK YOU

Quote:
Originally Posted by Dev69 View Post
and if the player is not connected, just use the ++ to switch the winner to next playerid..
Is not this?

Code:
        
while (!IsPlayerConnected(rand))
{
     if(ONEVENT[rand]) rand = random(GetPlayerPoolSize());
}
EDIT: It does not work.

The function GetRandomWinner is not called or just does not pick me as a winner...
I need that it know if there is even only one player ONEVENT == true it choose him as a winner,
or get random one winner if there is more players ONEVENT.

Last edited by LowIQ; 05/06/2019 at 12:25 PM.
LowIQ is offline   Reply With Quote
Old 05/06/2019, 12:50 PM   #9
SiaReyes
Huge Clucker
 
SiaReyes's Avatar
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 323
Reputation: 22
Default Re: Random (Choose Player On Event)

pawn Code:
stock GetRandomWinner()
{
     new count = 0, rand, i;
     for( i = 0; i < MAX_PLAYERS; i++) if(IsPlayerConnected(i) && ONEVENT[i] == true)
     {
       count++;
       break; // break the loop with players who are in event.
     }
     if(count == 1) return rand = i; // if there is only 1 player, that player is stored in rand.
     if(count >= 1) rand = random(i); //  if there are more than 1 player, get random id(i)
         return rand ;
}

Try this
__________________
Offshowtime Fuck It!

Quote:
Originally Posted by Toroi View Post
blackmail people that join your server using their ip address as hostage

or get a job and play sa-mp for fun
SiaReyes is offline   Reply With Quote
Old 05/06/2019, 01:04 PM   #10
fordawinzz
Huge Clucker
 
Join Date: Jun 2011
Posts: 445
Reputation: 35
Default Re: Random (Choose Player On Event)

use this:
pawn Code:
GetRandomWinner()
{
    new playerid = random(GetPlayerPoolSize());

    while (!IsPlayerConnected(playerid) || !ONEVENT[playerid])
        playerid = random(GetPlayerPoolSize());

    return playerid;
}

then GetRandomWinner() function will return you the id of the randomly picked player. use it out of any loop.
fordawinzz is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Random choose SumX Scripting Help 6 17/09/2012 05:53 PM
[HELP] How to choose a random player? TheBetaFox Scripting Help 5 07/09/2011 08:05 PM
Choose a random player ? Kitten Help Archive 21 05/08/2010 11:53 AM
OnPlayerSpawn Random Player Choose>? Kitten Help Archive 1 28/07/2010 01:46 AM
Random choose player Nonameman Help Archive 3 03/06/2010 12:33 PM


All times are GMT. The time now is 09:23 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.