SA-MP Forums Archive
Pick a random business - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Pick a random business (/showthread.php?tid=589796)



Pick a random business - CalvinC - 22.09.2015

Hello,

I'm creating a script where i want to pick a random business out of all the existing businesses i have.
This is my current code to loop through businesses.
pawn Код:
new randombiz;
for(new i = 1; i < MAX_BUSINESSES; i++)
{
    if(BusinessInfo[i][ExteriorX] != 0)
    {
        randombiz = i;
        break;
    }
}
But I can't think of a way to randomly pick one of the business ID's.


Re: Pick a random business - rappy93 - 22.09.2015

I don't know if it will work for sure but you could try like this:

pawn Код:
for(new i = 1; i < MAX_BUSINESSES; i++)
{
    if(BusinessInfo[i][ExteriorX] != 0)
    {
        new randombiz = random(i);
        break;
    }
}



Re: Pick a random business - Lordzy - 22.09.2015

You can create an array to store existing business ids and then use random function.
pawn Код:
new
    temp_Array[MAX_BUSINESSES],
    temp_Counts = 0,
    temp_RandID
;
for(new i = 0; i< MAX_BUSINESSES; i++) {

    if(BusinessInfo[i][ExteriorX] != 0)
        temp_Array[temp_Counts++] = i;
}
temp_RandID = temp_Array[random(temp_Counts)];
Or you can do it by having a variable with existing business count and you can simply use random function up to the existing business counts.
pawn Код:
new
    temp_RandID = random(g_BusinessCounts); //Unless there's removal of business data in middle.
You can also use foreach's iterator functions to make it work better.


Re: Pick a random business - Sellize - 22.09.2015

Just continue to create a random number with sizeof MAX_BIZ and then check if it exists, if not continue to try another random biz id.

Can't post code since I'm on my phone but you should be able to figure this out


Re: Pick a random business - CalvinC - 22.09.2015

Quote:
Originally Posted by Sellize
Посмотреть сообщение
Just continue to create a random number with sizeof MAX_BIZ and then check if it exists, if not continue to try another random biz id.

Can't post code since I'm on my phone but you should be able to figure this out
Thanks, I'll do that.