This should work for what you want to do:
pawn Код:
// [ DEVELOPMENT GAMEMODE ]
// RULES:
#pragma dynamic 1000000
// INCLUDES:
#include <a_samp>
#include <sscanf2>
#include <zcmd>
// DEFINES:
#define MAX_NUMBERS 899999
#define OFFSET 100000
// VARIABLES:
new bool:gNumbers[MAX_NUMBERS] = false;
// MAIN:
main()
{
print("Development Mode: random_not_taken.amx");
}
// CALLBACKS:
public OnGameModeInit()
{
return 1;
}
public OnGameModeExit()
{
return 1;
}
// COMMANDS:
CMD:add(playerid, params[])
{
new index;
if(sscanf(params, "i", index)) return SendClientMessage(playerid, -1, "Usage: /add (index).");
if(gNumbers[index]) return SendClientMessage(playerid, -1, "Index is already in use.");
if(index < 0 || index >= MAX_NUMBERS) return SendClientMessage(playerid, -1, "You have entered an invalid index.");
gNumbers[index] = true;
new string[128];
format(string, sizeof(string), "%d has been marked as taken.", index + OFFSET);
SendClientMessage(playerid, -1, string);
return 1;
}
CMD:remove(playerid, params[])
{
new index;
if(sscanf(params, "i", index)) return SendClientMessage(playerid, -1, "Usage: /remove (index).");
if(!gNumbers[index]) return SendClientMessage(playerid, -1, "Index is not in use.");
if(index < 0 || index >= MAX_NUMBERS) return SendClientMessage(playerid, -1, "You have entered an invalid number.");
gNumbers[index] = false;
new string[128];
format(string, sizeof(string), "%d has been marked as available.", index + OFFSET);
SendClientMessage(playerid, -1, string);
return 1;
}
CMD:random(playerid, params[])
{
new string[128], tick[2], number;
tick[0] = GetTickCount();
number = RandomNotTaken();
tick[1] = GetTickCount();
format(string, sizeof(string), "Number %d is a free non-taken number (%d ms).", number, tick[1] - tick[0]);
SendClientMessage(playerid, -1, string);
return 1;
}
// FUNCTIONS:
stock RandomNotTaken()
{
new free_numbers[MAX_NUMBERS], count;
for(new i = 0; i < MAX_NUMBERS; i ++)
{
if(!gNumbers[i])
{
free_numbers[count] = i;
count ++;
}
}
new selected = free_numbers[random(count)] + OFFSET;
return selected;
}
Test with 5 numbers:
pawn Код:
// [ DEVELOPMENT GAMEMODE ]
// INCLUDES:
#include <a_samp>
#include <sscanf2>
#include <zcmd>
// DEFINES:
#define MAX_NUMBERS 5
// VARIABLES:
new bool:gNumbers[MAX_NUMBERS] = false;
// MAIN:
main()
{
print("Development Mode: random_not_taken.amx");
}
// CALLBACKS:
public OnGameModeInit()
{
return 1;
}
public OnGameModeExit()
{
return 1;
}
// COMMANDS:
CMD:add(playerid, params[])
{
new index;
if(sscanf(params, "i", index)) return SendClientMessage(playerid, -1, "Usage: /add (index).");
if(gNumbers[index]) return SendClientMessage(playerid, -1, "Index is already in use.");
if(index < 0 || index >= MAX_NUMBERS) return SendClientMessage(playerid, -1, "You have entered an invalid index.");
gNumbers[index] = true;
new string[128];
format(string, sizeof(string), "%d has been marked as taken.", index);
SendClientMessage(playerid, -1, string);
return 1;
}
CMD:remove(playerid, params[])
{
new index;
if(sscanf(params, "i", index)) return SendClientMessage(playerid, -1, "Usage: /remove (index).");
if(!gNumbers[index]) return SendClientMessage(playerid, -1, "Index is not in use.");
if(index < 0 || index >= MAX_NUMBERS) return SendClientMessage(playerid, -1, "You have entered an invalid number.");
gNumbers[index] = false;
new string[128];
format(string, sizeof(string), "%d has been marked as available.", index);
SendClientMessage(playerid, -1, string);
return 1;
}
CMD:random(playerid, params[])
{
new string[128];
format(string, sizeof(string), "Number %d is a free non-taken number.", RandomNotTaken());
SendClientMessage(playerid, -1, string);
return 1;
}
// FUNCTIONS:
stock RandomNotTaken()
{
new free_numbers[MAX_NUMBERS], count;
for(new i = 0; i < MAX_NUMBERS; i ++)
{
if(!gNumbers[i])
{
free_numbers[count] = i;
count ++;
}
}
new selected = free_numbers[random(count)];
return selected;
}