Raised This Month: $51 Target: $400
 12% 

[HOW TO] Retrieve random values from an array without retreiving the same twice


Post New Thread Reply   
 
Thread Tools Display Modes
jediZEr0
Member
Join Date: Jan 2009
Location: In the Middle of Nowhere
Old 05-17-2009 , 14:16   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #21

Yes I know that . I just don't know how to work with arrays. Your tutorial confused me a bit: I retrieved some random IDs but where are them? cs_set_user_team(index, CS_TEAM_CT). Where is "index"?
jediZEr0 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-17-2009 , 21:56   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #22

This will only return a player that has not yet been selected. Once all players have been selected you must call ResetPlayers() to be able to select players again; you could modify the code to make it automatically reset selected players when all players have been selected. If you are unsure how to do this just ask.

PHP Code:
new g_Selected;
new 
g_MaxPlayers;

g_MaxPlayers get_maxplayers();

public 
client_disconnect(id)
{
    
g_Selected &= ~( << (id-1) ) )
}

ResetSelected()
{
    
g_Selected 0;
}

GetRandomPlayer()
{
    new 
iPlayerBits;
    new 
i;
    
    
//Make a bitsum of connected player indexes. 
    //You can add additional checks here (bot,team,admin, etc).
    
for ( <= g_MaxPlayers i++ )
        if ( 
is_user_connected) )
            
iPlayerBits |= ( << ( ) );
    
    
//No players are connected.
    
if ( !iPlayerBits )
        return 
0;

    
//Keep looping until there is no players that haven't been selected.
    
while( iPlayerBits != g_Selected )
    {
        
//Get random player id
        
= ( random_numg_MaxPlayers ) - );
        
        
//If player is connected and has not yet been selected
        
if ( ( iPlayerBits & ( << ) ) && !( g_Selected & ( << ) ) )
        {
            
//Set bit in g_Selected bit-field so they will be ignored on the next call
            
g_Selected |= ( << );
            return ( 
);
        }
    }
    
    
//All players have already been selected.
    
return 0;

__________________

Last edited by Bugsy; 05-17-2009 at 22:33.
Bugsy is offline
Old 05-17-2009, 22:12
joaquimandrade
This message has been deleted by joaquimandrade. Reason: i'm wrong
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-17-2009 , 22:19   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #23

Quote:
Originally Posted by Bugsy View Post
This will only return a player that has not yet been selected. Once all players have been selected you must call ResetPlayers() to be able to select players again; you could modify the code to make it automatically reset selected players when all players have been selected. If you are unsure how to do this just ask.

PHP Code:
new g_Selected;
new 
g_MaxPlayers;

g_MaxPlayers get_maxplayers();

ResetSelected()
{
    
g_Selected 0;
}

GetRandomPlayer()
{
    new 
iPlayerBits;
    new 
i;
    
    
//Make a bitsum of connected player indexes. 
    //You can add additional checks here (bot,team,admin, etc).
    
for ( <= g_MaxPlayers i++ )
        if ( 
is_user_connected) )
            
iPlayerBits |= ( << ( ) );
    
    
//No players are connected.
    
if ( !iPlayerBits )
        return 
0;

    
//Keep looping until there is no players that haven't been selected.
    
while( iPlayerBits != g_Selected )
    {
        
//Get random player id
        
= ( random_numg_MaxPlayers ) - );
        
        
//If player is connected and has not yet been selected
        
if ( ( iPlayerBits & ( << ) ) && !( g_Selected & ( << ) ) )
        {
            
//Set bit in g_Selected bit-field so they will be ignored on the next call
            
g_Selected |= ( << );
            return ( 
);
        }
    }
    
    
//All players have already been selected.
    
return 0;


If one or more selected players get disconnected and there is one or more connected player, all already selected, the loop will be infinite.
__________________
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-17-2009 , 22:34   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #24

Fixed, maybe.
__________________
Bugsy is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-17-2009 , 22:39   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #25

Quote:
Originally Posted by Bugsy View Post
Fixed, maybe.
Yes. How I would implement the final part:

PHP Code:
new g_Selected;
new 
g_MaxPlayers;

g_MaxPlayers get_maxplayers();

public 
client_disconnect(id)
{
    
g_Selected &= ~( << (id-1) ) )
}

GetRandomPlayer()
{
    new 
iPlayerBits;
    new 
i;
    
    
//Make a bitsum of connected player indexes. 
    //You can add additional checks here (bot,team,admin, etc).
    
for ( <= g_MaxPlayers i++ )
        if ( 
is_user_connected) )
            
iPlayerBits |= ( << ( ) );
    
    
//No players are connected.
    
if ( !iPlayerBits )
        return 
0;
    else if( 
iPlayerBits == g_Selected )
        
g_Selected 0

    
//Keep looping until there is no players that haven't been selected.
    
do
    {
        
//Get random player id
        
= ( random_numg_MaxPlayers ) - );
        
        
//If player is connected and has not yet been selected
        
if ( ( iPlayerBits & ( << ) ) && !( g_Selected & ( << ) ) )
        {
            
//Set bit in g_Selected bit-field so they will be ignored on the next call
            
g_Selected |= ( << );
            return ( 
);
        }
    }
    while(
true)
    
    
//All players have already been selected.
    
return 0;

or

PHP Code:

new g_MaxPlayers;
new 
g_iPlayerBits
new g_Selected

public plugin_cfg()
{
     
g_MaxPlayers get_maxplayers();
}

public 
client_connect(id)
{
    
g_iPlayerBits |= ( << (id 1) );
}
public 
client_disconnect(id)
{
    
g_iPlayerBits &= ~( << (id-1) ) )
    
g_Selected &= ~( << (id-1) ) )
}

GetRandomPlayer()
{
    
//At least, one player is connected.
    
if (iPlayerBits)
    {
        if( 
iPlayerBits == g_Selected )
           
g_Selected 0

        
new i;

        
//Keep looping until there is no players that haven't been selected.
        
do
        {
            
//Get random player id
            
= ( random_numg_MaxPlayers ) - );
        
            
//If player is connected and has not yet been selected
            
if ( ( iPlayerBits & ( << ) ) && !( g_Selected & ( << ) ) )
            {
                
//Set bit in g_Selected bit-field so they will be ignored on the next call
                
g_Selected |= ( << );
                return ( 
);
            }
        }
        while(
true)
    }    
    
//All players have already been selected.
    
return 0;

__________________

Last edited by joaquimandrade; 05-17-2009 at 22:56.
joaquimandrade is offline
jediZEr0
Member
Join Date: Jan 2009
Location: In the Middle of Nowhere
Old 05-18-2009 , 07:21   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #26

Quote:
Originally Posted by joaquimandrade View Post

PHP Code:

        
//Get random player id
        
= ( random_numg_MaxPlayers ) - ); 
So it's going to be cs_set_user_team(i, CS_TEAM_CT) ?
jediZEr0 is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-18-2009 , 11:53   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #27

Quote:
Originally Posted by jediZEr0 View Post
So it's going to be cs_set_user_team(i, CS_TEAM_CT) ?
You would have to use something like this in your code:

PHP Code:
new player GetRandomPlayer()

if(
player)
{
    
cs_set_user_team(player,CS_TEAM_CT)

__________________
joaquimandrade is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 05-18-2009 , 14:51   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #28

Updated with the "better looking" loop and more efficient way of removing the random index.
Added the Sort*() function from AMXX as shown by P34nut and arkshine.
Added 2 examples of retrieving random players.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-18-2009 , 17:47   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #29

@joa

I left the resetting of already selected players up to the user because some may only want to issue a command on a set of players (on a random basis) once; they can then reset selected-players manually if they would like to again issue commands on a random basis. Automatically resetting will always return a player even if the player has already been selected once prior.
__________________
Bugsy is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-18-2009 , 18:20   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #30

Quote:
Originally Posted by Bugsy View Post
@joa

I left the resetting of already selected players up to the user because some may only want to issue a command on a set of players (on a random basis) once; they can then reset selected-players manually if they would like to again issue commands on a random basis. Automatically resetting will always return a player even if the player has already been selected once prior.
Automatic resetting will just return an already selected player if there are no more players to select so, i'm not seeing what would be the difference.
__________________
joaquimandrade is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 00:00.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode