This is efficient?
#1

Hello, I am making a system to get a random business, but I do not know if my code will be efficient.
In this I am looking for a business with certain characteristics so that it fits.

PHP код:
FindBusiness(playerid)
{
    new 
random(ValidBusiness); // Total business
    
    
if(BusinessData[i][bizExists] && BusinessData[i][bizType] >= && BusinessData[i][bizType] <= 4  && GetPlayerVirtualWorld(playerid) == BusinessData[i][bizExteriorVW] && GetPlayerInterior(playerid) == BusinessData[i][bizExterior])
    {
        if (
BusinessData[i][bizDeliver][0] != 0.0 && BusinessData[i][bizDeliver][1] != 0.0 && BusinessData[i][bizDeliver][2] != 0.0)
        {
            print(
"found!");
            return 
i;
        }
    }
    print(
"search again.");
    return 
FindBusiness(playerid);

Reply
#2

PHP код:
print("search again.");
    return 
FindBusiness(playerid); 
This code will always be executed. to avoid it try it like this:

PHP код:
FindBusiness(playerid)
{
    new 
random(ValidBusiness); // Total business
    
    
if(BusinessData[i][bizExists] && BusinessData[i][bizType] >= && BusinessData[i][bizType] <= 4  && GetPlayerVirtualWorld(playerid) == BusinessData[i][bizExteriorVW] && GetPlayerInterior(playerid) == BusinessData[i][bizExterior])
    {
        if (
BusinessData[i][bizDeliver][0] != 0.0 && BusinessData[i][bizDeliver][1] != 0.0 && BusinessData[i][bizDeliver][2] != 0.0)
        {
            print(
"found!");
            return 
i;
        }
    }
    else
    {
      print(
"search again.");
      return 
FindBusiness(playerid);       
    }
    

But... if you're going through valid bussiness only then it should always find it.
Reply
#3

Quote:
Originally Posted by iLearner
Посмотреть сообщение
PHP код:
print("search again.");
    return 
FindBusiness(playerid); 
This code will always be executed. to avoid it try it like this:

PHP код:
FindBusiness(playerid)
{
    new 
random(ValidBusiness); // Total business
    
    
if(BusinessData[i][bizExists] && BusinessData[i][bizType] >= && BusinessData[i][bizType] <= 4  && GetPlayerVirtualWorld(playerid) == BusinessData[i][bizExteriorVW] && GetPlayerInterior(playerid) == BusinessData[i][bizExterior])
    {
        if (
BusinessData[i][bizDeliver][0] != 0.0 && BusinessData[i][bizDeliver][1] != 0.0 && BusinessData[i][bizDeliver][2] != 0.0)
        {
            print(
"found!");
            return 
i;
        }
    }
    else
    {
      print(
"search again.");
      return 
FindBusiness(playerid);       
    }
    

But... if you're going through valid bussiness only then it should always find it.
But the function must end with a return.
Reply
#4

return 1;
Reply
#5

Quote:
Originally Posted by iLearner
Посмотреть сообщение
PHP код:
print("search again.");
    return 
FindBusiness(playerid); 
This code will always be executed. to avoid it try it like this:

PHP код:
FindBusiness(playerid)
{
    new 
random(ValidBusiness); // Total business
    
    
if(BusinessData[i][bizExists] && BusinessData[i][bizType] >= && BusinessData[i][bizType] <= 4  && GetPlayerVirtualWorld(playerid) == BusinessData[i][bizExteriorVW] && GetPlayerInterior(playerid) == BusinessData[i][bizExterior])
    {
        if (
BusinessData[i][bizDeliver][0] != 0.0 && BusinessData[i][bizDeliver][1] != 0.0 && BusinessData[i][bizDeliver][2] != 0.0)
        {
            print(
"found!");
            return 
i;
        }
    }
    else
    {
      print(
"search again.");
      return 
FindBusiness(playerid);       
    }
    

But... if you're going through valid bussiness only then it should always find it.
Both are same and it won't get executed if the if statement get false. And if that become true it will return i and recursion part won't get executed.
Reply
#6

I would create new array. Loop thru businesses and save ones that fit to that new array. Then i would know HOW many of them fit criteria and i could use random function to chose one randomly...
Reply
#7

Well, it really depends on the amount of businesses you actually have vs. the amount of max businesses defined in the script.

If you have a max business value of 100, while you only have 15 valid businesses you'll have a chance of waiting a very long time before the randomizer finally hits one of the valid businesses, it can take up to minutes if not hours or days. I would rather grab a random value based on an enum length, so in this case you'll end up like this:

PHP код:
new random(sizeof(BusinessData)); 
I haven't tested it, but in theory this should work a lot more efficient, once I have the chance to do so I'll do some benchmarking tests comparing your method with mine.
Reply
#8

It would be better to store the current VALID businesses by making a variable and storing the amount in it. The amount could be increased in the loading and creation process. This would be better than using new i = random(sizeof BusinessData); + There is a runtime error here (if I'm not wrong) << add - 1 in the code.
Reply
#9

Quote:
Originally Posted by Duco
Посмотреть сообщение
it can take up to minutes if not hours or days.
Theoretically, yes. Practically, no.

The most efficient way would still be a custom foreach iterator.
Reply
#10

Quote:
Originally Posted by Vince
Посмотреть сообщение
Theoretically, yes. Practically, no.

The most efficient way would still be a custom foreach iterator.
I have an interator already, the thing is that I do not want a random business of the total business created, I want a random business with certain characteristics of them, for example: have a certain type or are located somewhere
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)