Pick a random business
#1

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.
Reply
#2

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

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.
Reply
#4

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
Reply
#5

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)