AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Randomly choose item only once (https://forums.alliedmods.net/showthread.php?t=131869)

SpeeDeeR 07-09-2010 19:50

Randomly choose item only once
 
So lets say i have twenty items and i have to draw one and there is random chance for that.So far so good.But the thing is, that every item must be chosen once.And to clear things up this way there has to be twenty different draws with one different item.

Bugsy 07-09-2010 20:43

Re: Randomly choose item only once
 
Quote:

Originally Posted by SpeeDeeR (Post 1233652)
So lets say i have twenty items and i have to draw one and there is random chance for that.So far so good.But the thing is, that every item must be chosen once.And to clear things up this way there has to be twenty different draws with one different item.

This will give you a random number ranging from 1-20, with each only being returned once. After the twentieth item is used\selected, the selected-numbers will be cleared and you will be able to obtain a new set of 1-20, again each being returned only once. I'm not sure if you were using 1-20 just for example purposes or if it is what you actually needed. Let me know and I can fix it.

PHP Code:

RandomNumber()
{
    static 
iSelected iRandom;
    
    if ( 
iSelected == 0x1FFFFE )
        
iSelected 0;
        
    for ( ;; )
    {
        
iRandom random_num20 );
        
        if ( !( 
iSelected & ( << iRandom ) ) )
        {
            
iSelected |= ( << iRandom );
            return 
iRandom;
        }
    }
    
    return 
0;




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

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