I know this is already answered but I was bored and put this together.
You must first call PrepRandom() for any min/max combo prior to calling GetRandom() for that min/max combo. Once you call PrepRandom(), you can repeatedly call GetRandom() and you will always get a unique random until all numbers have been used. Once all are used, you will then begin getting the numbers again, and they will be unique until you again use up all the numbers.
Like this. But you cannot cannot call GetRandom on a particular min/max until you first call PrepRandom for that min/max set.
PHP Code:
PrepRandom( 1 , 10 );
i = GetRandom( 1 , 10 );
i = GetRandom( 1 , 10 );
i = GetRandom( 1 , 10 );
i = GetRandom( 1 , 10 );
PrepRandom( 5 , 500 );
i = GetRandom( 5 , 500 );
i = GetRandom( 5 , 500 );
i = GetRandom( 5 , 500 );
i = GetRandom( 5 , 500 );
PHP Code:
#include <amxmodx>
new const Version[] = "0.1";
//Set this to the absolute max random number that you will attempt to generate.
const MaxRandom = 20;
new AllNumbers[ ( MaxRandom >> 5 ) + 1 ];
public plugin_init()
{
register_plugin( "Random Number No Dupes" , Version , "bugsy" );
PrepRandom( 1 , 20 );
for ( new i = 1 ; i <= 100 ; i++ )
server_print( "[%d] Random=%d" , i , GetRandom( 1 , 20 ) );
}
PrepRandom( iRanMin , iRanMax )
{
for ( new i = iRanMin ; i <= iRanMax ; i++ )
AllNumbers[ i >> 5 ] |= ( 1 << i );
}
public GetRandom( iRanMin , iRanMax )
{
static UsedNumbers[ ( MaxRandom >> 5 ) + 1 ];
new iRandom , bool:bAllUsed=true;
if ( iRanMax > MaxRandom )
set_fail_state( "Must increase MaxRandom value: %d > %d" , iRanMax , MaxRandom );
//Check if all numbers have already bee nused, if so this will reset the 'used' variable.
for ( new i = 0 ; i < sizeof( UsedNumbers ) ; i++ )
{
if ( AllNumbers[ i ] && ( AllNumbers[ i ] != UsedNumbers[ i ] ) )
{
bAllUsed = false;
break;
}
}
//If all random numbers have already been used, reset variable so they can be re-used.
if ( bAllUsed )
arrayset( UsedNumbers , 0 , sizeof( UsedNumbers ) );
//Find random number
do
{
iRandom = random_num( iRanMin , iRanMax );
}
while ( ( UsedNumbers[ iRandom >> 5 ] & ( 1 << iRandom ) ) )
//Set current random number as used
UsedNumbers[ iRandom >> 5 ] |= ( 1 << iRandom );
//Return random number
return iRandom;
}
__________________