Best way to make sure this number isn't the same as any others?
#8

Quote:
Originally Posted by kvann
Посмотреть сообщение
A lot easier and faster way than creating another array:

pawn Код:
randomin(min, max) return random(max - min) + min;

bool:NumberInUse(number)
{
        for (new i; i < MAX_FURNI; i++) if (FurniCode[i] == number) return false;
        return true;
}

FreeNumber()
{
    new number = randomin(100000, 1000000);
    while (NumberInUse(number)) number = randomin(100000, 1000000);
    return number;
}
Let's see your and my version in action. Mine being 'Version 1' and yours being 'Version 2'.



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;
}
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 4 Guest(s)