SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 22/09/2015, 04:22 PM   #1
CalvinC
High-roller
 
CalvinC's Avatar
 
Join Date: Nov 2014
Location: Denmark
Posts: 1,831
Reputation: 114
Default Pick a random business

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 Code:
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.
__________________
[Tutorial] Custom Playerdamage system
Desert Eagle's dealing too much damage, you want one-hit sniper kills, or damage based on body parts? Check this out.
CalvinC is offline   Reply With Quote
Old 22/09/2015, 04:44 PM   #2
rappy93
Huge Clucker
 
rappy93's Avatar
 
Join Date: Mar 2012
Location: Romania
Posts: 449
Reputation: 18
Default Re: Pick a random business

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

pawn Code:
for(new i = 1; i < MAX_BUSINESSES; i++)
{
    if(BusinessInfo[i][ExteriorX] != 0)
    {
        new randombiz = random(i);
        break;
    }
}
__________________
Nothing.
rappy93 is offline   Reply With Quote
Old 22/09/2015, 05:11 PM   #3
Lordzy
High-roller
 
Lordzy's Avatar
 
Join Date: Mar 2012
Location: NetherRealm
Posts: 2,701
Reputation: 1177
Default Re: Pick a random business

You can create an array to store existing business ids and then use random function.
pawn Code:
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 Code:
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.
__________________
Currently inactive - I don't play at any SA-MP servers nor work on anything in PAWN for now. The projects that I've done so far in PAWN, which requires updates will be taking some time.
Lordzy is offline   Reply With Quote
Old 22/09/2015, 05:13 PM   #4
Sellize
Gangsta
 
Sellize's Avatar
 
Join Date: Jan 2013
Location: (ง ͠ ͟ل͜ ͡)ง fight me irl
Posts: 709
Reputation: 293
Default Re: Pick a random business

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
__________________
dead
Sellize is offline   Reply With Quote
Old 22/09/2015, 05:44 PM   #5
CalvinC
High-roller
 
CalvinC's Avatar
 
Join Date: Nov 2014
Location: Denmark
Posts: 1,831
Reputation: 114
Default Re: Pick a random business

Quote:
Originally Posted by Sellize View Post
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.
__________________
[Tutorial] Custom Playerdamage system
Desert Eagle's dealing too much damage, you want one-hit sniper kills, or damage based on body parts? Check this out.
CalvinC is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Team Pick Shockey HD Scripting Help 2 28/08/2011 05:52 AM
help/pick a random player RowdyrideR Help Archive 4 09/07/2011 02:30 PM
pick a random guy? MrIncredible Help Archive 5 11/01/2011 11:22 PM
How do I pick a random admin? biltong Help Archive 4 06/04/2010 10:55 AM
random pick sggassasin Help Archive 8 19/07/2009 12:00 PM


All times are GMT. The time now is 09:47 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.