Random number, then pick string out of an array depending on number?
#2

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


Messages In This Thread
Random number, then pick string out of an array depending on number? - by Robbin237 - 26.02.2009, 14:06
Re: Random number, then pick string out of an array depending on number? - by Nero_3D - 26.02.2009, 15:11
Re: Random number, then pick string out of an array depending on number? - by Nero_3D - 26.02.2009, 18:26

Forum Jump:


Users browsing this thread: 3 Guest(s)