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

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


Post New Thread Reply   
 
Thread Tools Display Modes
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 08-05-2011 , 14:49   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #61

Using more brackets will do the job? Right?
Code:
new count; public plugin_natives( ) {     register_native( "increase", "_increase" ); } public _increase( ) {
    return (count++);
}
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-05-2011 , 14:52   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #62

Quote:
Originally Posted by abdul-rehman View Post
Using more brackets will do the job? Right?
Code:
new count; public plugin_natives( ) {     register_native( "increase", "_increase" ); } public _increase( ) {
    return (count++);
}
Parenthesis don't really do much in Pawn and wouldn't help in this case.
In fact, parenthesis aren't needed half of the time. It just looks better to use them.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 05-07-2012 , 22:46   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #63

nice work dude! I wouldn't have though of using a do-while. Mostly cause i'm stooped.
Liverwiz is offline
JusTGo
Veteran Member
Join Date: Mar 2013
Old 12-27-2015 , 15:39   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #64

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();

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;

plz make it automatically reset selected players when all players have been selected.
__________________
JusTGo is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 12-28-2015 , 07:30   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #65

I've noticed that returning an increment variable ( return var++; ) returns garbage values a long time ago. So, right now, I decided to check the generated assembly. It really is a compiler bug! This code:
PHP Code:
public plugin_init() {
    for(new 
05i++) {
        
server_print("(%i) Test1: %i Test2: %i"iTest1(), Test2());
    }
}

Test1() {
    
#emit CONST.pri 1337
    
static var;
    return var++;
}

Test2() {
    static var;
    var++;
    return var;

Produces following output in server console:
Code:
(0) Test1: 1337 Test2: 1
(1) Test1: 1337 Test2: 2
(2) Test1: 1337 Test2: 3
(3) Test1: 1337 Test2: 4
(4) Test1: 1337 Test2: 5
Assembly for function Test1():
Code:
PROC			; stock Test1()
CONST.pri	0x539	; #emit CONST.pri 1337
INC		0xDC	; var
RETN
In PAWN, returned values are stored into the PRI register, and what happens here is that when you return an incremented value, the value never gets copied to the PRI register! As you can see, the value I put into PRI (1337) persists and is returned every time, even though it should get overridden by the value of "var".
So, when you return an incremented value, whatever was left in PRI (which could basically be anything) will get returned.

P.S. I know I am late with this reply several years, but whatever, someone might find this information useful.

Last edited by klippy; 12-28-2015 at 07:31.
klippy is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 12-28-2015 , 08:11   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #66

Without forcing PRI, returned value are correct, or am I missing something?
__________________

Last edited by Arkshine; 12-28-2015 at 08:13.
Arkshine is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 12-28-2015 , 11:05   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #67

Quote:
Originally Posted by Arkshine View Post
Without forcing PRI, returned value are correct, or am I missing something?
Maybe the adding of that #emit directive was too confusing, sorry about that. I did it only just to prove this:
Quote:
So, when you return an incremented value, whatever was left in PRI (which could basically be anything) will get returned.
The value of variable "var" is supposed to get loaded into the PRI register, but the function returns whatever was in PRI, not the variable value.
klippy is offline
Old 01-04-2016, 22:44
hleV
This message has been deleted by hleV.
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

Forum Jump


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


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