18.07.2015, 16:08
(
Последний раз редактировалось SickAttack; 18.07.2015 в 19:20.
)
Quote:
A lot easier and faster way than creating another array:
pawn Код:
|
As you can see yours is not faster and it is vastly unstable to even consider using it.
Code for anyone that wants to test them both:
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_identifier_benchmark.amx");
}
// CALLBACKS:
public OnGameModeInit()
{
for(new i = 0; i < MAX_NUMBERS; i ++)
{
if(i == 529428) continue;
gNumbers[i] = true;
}
return 1;
}
public OnGameModeExit()
{
return 1;
}
// COMMANDS:
CMD:random_1(playerid, params[])
{
new string[128], tick[2], number;
tick[0] = GetTickCount();
number = RandomNotTaken_1();
tick[1] = GetTickCount();
format(string, sizeof(string), "Number %d is a free non-taken number (%d ms). Version 1.", number, tick[1] - tick[0]);
SendClientMessage(playerid, -1, string);
return 1;
}
CMD:random_2(playerid, params[])
{
new string[128], tick[2], number;
tick[0] = GetTickCount();
number = RandomNotTaken_2();
tick[1] = GetTickCount();
format(string, sizeof(string), "Number %d is a free non-taken number (%d ms). Version 2.", number, tick[1] - tick[0]);
SendClientMessage(playerid, -1, string);
return 1;
}
// FUNCTIONS:
stock RandomNotTaken_1()
{
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;
}
stock RandomNotTaken_2()
{
new number, free_number;
for(new i = 0; i < 2; i ++)
{
number = RandomBetween(100000, 899999) - OFFSET;
if(gNumbers[number])
{
i = 0;
}
else
{
free_number = number + OFFSET;
break;
}
}
return free_number;
}
stock RandomBetween(minimum, maximum)
{
new selected = random(maximum - minimum) + minimum;
return selected;
}