Raised This Month: $ Target: $400
 0% 

random function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
tripc
Senior Member
Join Date: Aug 2006
Old 09-11-2009 , 10:38   random function
Reply With Quote #1

I want to get ten random numbers between 1-20
PHP Code:
random_num(1,20
but I want to get 10 numbers and the numbers will not return them self
that i'll get ten numbers in random order
tripc is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 09-11-2009 , 10:45   Re: random function
Reply With Quote #2

Maybe something like this...

PHP Code:
public Myfunc( ) {
    new 
iNumbers10 ];
    
    
GetRandomNumbersiNumbers );
}

stock GetRandomNumbersiOutput10 ] ) {
    new 
jiRandomiDonebool:bNext, Array:aNums ArrayCreate( );
    
    for( new 
i10i++ ) {
        
bNext false;
        
        
iRandom random_num120 );
        
        for( 
0iDonej++ ) {
            if( 
ArrayGetCellaNums) == iRandom ) {
                
bNext true;
                
                break;
            }
        }
        
        if( 
bNext )
            continue;
        
        
ArrayPushCellaNumsiRandom );
        
        
iOutputiDone ] = iRandom;
        
        
iDone++;
    }
    
    
ArrayDestroyaNums );

EDIT: Uhm... this wont work properly, i am lazy to fix it now, maybe someone can
__________________

Last edited by xPaw; 09-11-2009 at 16:38.
xPaw is offline
zacky
Senior Member
Join Date: Mar 2008
Location: Sweden
Old 09-11-2009 , 11:57   Re: random function
Reply With Quote #3

I don't know if this will work
But can't you do this?
PHP Code:
new iNumbers[10];

for (new 
i10i++) {
    
iNumbers[i] = random_num(120);

zacky is offline
Send a message via Skype™ to zacky
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 09-11-2009 , 12:16   Re: random function
Reply With Quote #4

He dont want the numbers to be twice or more
__________________
xPaw is offline
zacky
Senior Member
Join Date: Mar 2008
Location: Sweden
Old 09-11-2009 , 12:20   Re: random function
Reply With Quote #5

Quote:
Originally Posted by xPaw View Post
He dont want the numbers to be twice or more
Where did you get that from?
He said he wanted 10 random numbers.
zacky is offline
Send a message via Skype™ to zacky
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 09-11-2009 , 13:51   Re: random function
Reply With Quote #6

Quote:
Originally Posted by zacky View Post
Where did you get that from?
He said he wanted 10 random numbers.
Quote:
Originally Posted by tripc View Post
but I want to get 10 numbers and the numbers will not return them self
From that. It's understandable even not clear.
SnoW is offline
Send a message via MSN to SnoW
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 09-11-2009 , 14:02   Re: random function
Reply With Quote #7

You guys are taking the wrong approach.

Code:
new iNumbers[ 10 ]; GetRandomNumbers( 1, 20, iNumbers, 10 ); GetRandomNumbers( const iStart, const iStop, iResults[ ], const iSize ) {     if( iStart >= iStop )     {         return 0;     }         new Array:aNumbers = ArrayCreate( 1 );     new iArraySize = iStop - iStart + 1;         for( new i = 0; i < iArraySize; i++ )     {        ArrayPushCell( aNumbers, i + iStart );     }         new iLastIndex = min( iArraySize, iSize );     new iRand;     for( new i = 0; i < iLastIndex; i++ )     {         iRand = random( iArraySize );                 iResults[ i ] = ArrayGetCell( aNumbers, iRand );         ArrayDeleteItem( aNumbers, iRand );                 iArraySize--;     }         ArrayDestroy( aNumbers );         return iLastIndex; }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 09-11-2009 , 15:42   Re: random function
Reply With Quote #8

Code:
new iNumbers[10]
for (new i=0; i < 10; i++)
{
iNumbers[i] = random_num(1, 20)
for (new j=0; 1 < i; j++)
{
if (iNumbers[i] == iNumbers[j] && i != j)
i = i-1
}
}
Correct me if i'm wrong..
Mxnn is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 09-11-2009 , 15:44   Re: random function
Reply With Quote #9

Yes you are wrong.
Your code does unnecessary checks and could execute more loops than needed.
See my code.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 09-11-2009 , 16:12   Re: random function
Reply With Quote #10

But my code repeat the loop if the random name was obtain before, else the loop runs normally
Mxnn 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 05:44.


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