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
PvtSmithFSSF
Senior Member
Join Date: Jul 2008
Old 07-23-2008 , 22:47   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #11

X-olent you rock
PvtSmithFSSF is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 07-24-2008 , 05:00   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #12

Quote:
Originally Posted by X-olent View Post
retrieve would also have to be a constant number.
I haven't said anything really stupid for a while. I guess it was due.
__________________
No support via PM.
Lee is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-10-2009 , 20:41   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #13

This line is nice:

PHP Code:
for( new rand random(total); count retrieverand random(total) ) 
About the rest:

You dont need to re-sort the initial array since if you want to get a random group of players you wont use it anymore. So you can do it like this:
PHP Code:
for( new rand random(total); count retrieverand random(total) )
{
    
selected[count++] = players[rand];    
    
players[rand] = player[--total]

__________________
joaquimandrade is offline
stupok
Veteran Member
Join Date: Feb 2006
Old 05-10-2009 , 21:15   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #14

Quote:
Originally Posted by joaquimandrade View Post
PHP Code:
for( new rand random(total); count retrieverand random(total) )
{
    
selected[count++] = players[rand];    
    
players[rand] = player[--total]

It's really annoying for me to process this with my feeble brain, so I'm going to outline an example for myself (and others that want to understand the code).

EDIT: the below has been fixed

Simplified:

Code:
new players[3] = {1,2,3} new selected[3] new count = 0 new retrieve = 3 new total = 3 // random(3) returns 0, 1, or 2 for( new rand = random(total); count < retrieve; rand = random(total) ) {     selected[count++] = players[rand];        players[rand] = players[--total] } /* Here we go! ITERATION 1 rand = 2, count = 0 and 0 < 3, (rand can be 0, 1, or 2) selected[count] = players[rand], selected[0] = players[2], so selected[0] = 3 count = 1 total = 2 players[rand] = players[total], players[2] = players[2], so players[2] = 3 ITERATION 2 rand = 1, count = 1 and 1 < 3, (rand can be 0 or 1) selected[count] = players[rand], selected[1] = players[1], so selected[1] = 2 count = 2 total = 1 players[rand] = players[total], players[1] = players[1], so players[1] = 2 ITERATION 3 rand = 0, count = 2 and 2 < 3, (rand can be 0) selected[count] = players[rand], selected[2] = players[0], so selected[2] = 1 count = 3 total = 0 players[rand] = players[total], players[0] = players[0], so players[0] = 1 END count = 3 and 3 < 3 is false selected is {3,2,1} */

Unless I've overlooked something, selected[2] ends with the same value as selected[0], so this code won't work.
__________________

Last edited by stupok; 05-10-2009 at 22:19.
stupok is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-10-2009 , 21:23   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #15

Quote:
Originally Posted by stupok View Post
It's really annoying for me to process this with my feeble brain, so I'm going to outline an example for myself (and others that want to understand the code).

Simplified:

Code:
new players[3] = {1,2,3} new selected[3] new count = 0 new retrieve = 3 new total = 3 // random(3) returns 0, 1, or 2 for( new rand = random(total); count < retrieve; rand = random(total) ) { selected[count++] = players[rand];
players[rand] = players[--total] } /* Here we go! ITERATION 1 rand = 2, count = 0 and 0 < 3 selected[count] = players[rand], selected[0] = players[2], so selected[0] = 3 count = 1 total = 2 players[rand] = players[total], players[2] = players[2], so players[2] = 3 ITERATION 2 rand = 1, count = 1 and 1 < 3 selected[count] = players[rand], selected[1] = players[1], so selected[1] = 2 count = 2 total = 1 players[rand] = players[total], players[1] = players[1], so players[1] = 2 ITERATION 3 rand = 2, count = 2 and 2 < 3 selected[count] = players[rand], selected[2] = players[2], so selected[2] = 3
count = 3 total = 0 players[rand] = players[total], players[2] = players[0], so players[2] = 1 END count = 3 and 3 < 3 is false */



Unless I've overlooked something, selected[2] ends with the same value as selected[0], so this code won't work.
The total is decreasing so in the last iteration you can't have random = 2. It will be always 0.

Edit: I mean, it will always be 0 when the initial total = retrieve
__________________

Last edited by joaquimandrade; 05-10-2009 at 21:52.
joaquimandrade is offline
stupok
Veteran Member
Join Date: Feb 2006
Old 05-10-2009 , 22:04   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #16

So, I did overlook something. Damn, this code is good.
__________________
stupok is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 05-11-2009 , 01:03   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #17

Quote:
Originally Posted by joaquimandrade View Post
PHP Code:
for( new rand random(total); count retrieverand random(total) )
{
    
selected[count++] = players[rand];    
    
players[rand] = player[--total]

Instead of a kind of "ugly" for loop, I prefer:

PHP Code:
new rand;
do {
    
rand random(total);
    
selected[count++] = players[rand];    
    
players[rand] = player[--total];
}
while( 
count retrieve 
It's purely stylistic but IMO it looks much better.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-11-2009 , 01:19   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #18

Quote:
Originally Posted by Emp` View Post
Instead of a kind of "ugly" for loop, I prefer:

PHP Code:
new rand;
do {
    
rand random(total);
    
selected[count++] = players[rand];    
    
players[rand] = player[--total];
}
while( 
count retrieve 
It's purely stylistic but IMO it looks much better.
I was thinking about that and in fact it is better like that. With the for loop it is done a unnecessary rand = random(total) in the end of the final iteration.
__________________
joaquimandrade is offline
Old 05-15-2009, 19:56
[ --<-@ ] Black Rose
This message has been deleted by [ --<-@ ] Black Rose.
jediZEr0
Member
Join Date: Jan 2009
Location: In the Middle of Nowhere
Old 05-17-2009 , 07:49   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #19

How can I move the players with the retrieved IDs to a team?
jediZEr0 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 05-17-2009 , 12:57   Re: [HOW TO] Retrieve random values from an array without retreiving the same twice
Reply With Quote #20

If you are using Counter-Strike, you can use the cs_set_user_team() native.
http://www.amxmodx.org/funcwiki.php
http://www.amxmodx.org/funcwiki.php?...team&go=search
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] 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 09:17.


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