I would probably use the functions provided by Bugsy
here. Doing something like this:
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 = 32;
new AllNumbers[ ( MaxRandom >> 5 ) + 1 ];
public plugin_init()
{
register_plugin( "Random Number No Dupes" , Version , "bugsy/fysiks" );
register_concmd("randplayers", "cmdGetRandomPlayers", .info=" - <number of random players>")
}
public cmdGetRandomPlayers(id)
{
new iPlayers[32], iPlayerCount, szName[32]
get_players(iPlayers, iPlayerCount)
new szArg[3], iCount
read_argv(1, szArg, charsmax(szArg))
iCount = min(str_to_num(szArg), iPlayerCount)
PrepRandom(0 , iPlayerCount - 1);
for( new i = 0 ; i < iCount ; i++ )
{
get_user_name(GetRandom(0, iPlayerCount - 1), szName, charsmax(szName))
console_print(id, "Random player: %s", szName);
}
}
// Source: https://forums.alliedmods.net/showpost.php?p=2417667&postcount=7
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;
}
I'm not able to test it but I think it's mostly there.
__________________