SA-MP Forums

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

 
 
Thread Tools Display Modes
Old 26/02/2009, 03:06 PM   #1
Robbin237
Big Clucker
 
Join Date: Nov 2007
Posts: 170
Reputation: 0
Default Random number, then pick string out of an array depending on number?

Ok so what i want is that i make a random number between 1 and 5 with my own function

rand(1, 5) --- duh

Then we have an array here:

pawn Code:
new Names[] = {"Michael", "John", "Katey", "Richard", "Robbin"};

Then if it outputs 5 or any other number, select that string from the array.
Like if rand(1, 5) outputs 2, then send the player a message with the name John in it.
If it outputs 3, send the player a message with the name Katey in it.

Can somebody explain me how?

Thanks!
Robbin237 is offline  
Old 26/02/2009, 04:11 PM   #2
Nero_3D
High-roller
 
Nero_3D's Avatar
 
Join Date: Jun 2007
Location: Germany
Posts: 2,838
Reputation: 671
Default Re: Random number, then pick string out of an array depending on number?

First this wont work
pawn Code:
new Names[] = {"Michael", "John", "Katey", "Richard", "Robbin"};
because you create an array for arrays also you need two dimensions not one
pawn Code:
new Names[][] =
{ //just made the brace around the names to see better that they are arrays too
    {"Michael"},
    {"John"},
    {"Katey"},
    {"Richard"},
    {"Robbin"}
};

Than to your random problem

pawn Code:
new string[128];
format(string, sizeof(string), "%s says hello to you!", Names[random(sizeof(Names))]);
SendClientMessage(playerid, 0xFFFFFFAA, string);
//we get the array size with sizeof(Names) - the size of the first dimension is 5
//so we do indirect random(5); which will return a number from -1 till 5 (0, 1, 2, 3 or 4)
//so when random as example return 3 it will be Names[3] and that is "Richard"
Nero_3D is offline  
Old 26/02/2009, 07:26 PM   #3
Nero_3D
High-roller
 
Nero_3D's Avatar
 
Join Date: Jun 2007
Location: Germany
Posts: 2,838
Reputation: 671
Default Re: Random number, then pick string out of an array depending on number?

never thought much about that but didnt you forgot the commas at your second pawn code ?

And that are for sure only two dimensions XD
pawn Code:
new Names[][] =
{
    {'M', 'i', 'c', 'h', 'a', 'e', 'l'},
    {'J', 'o', 'h', 'n'},
    {'K', 'a', 't', 'e', 'y'},
    {'R', 'i', 'c', 'h', 'a', 'r', 'd'},
    {'R', 'o', 'b', 'b', 'i', 'n'}
};
Nero_3D is offline  
 

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
Quick one about random number. IcyBlight Help Archive 2 03/06/2010 12:17 PM
Random Number Generator master000 Help Archive 2 03/03/2010 01:18 AM
[HELP] Random number addinol Help Archive 6 19/02/2010 11:00 PM
Number of strings in a string? Programie Help Archive 5 05/02/2010 07:34 PM


All times are GMT. The time now is 10:47 AM.


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