Feb 14, 2009 at 9:21pm UTC
I am trying to randomize an array of strings.
ex(like in a mad lib program)
I have them declared as
// CODE
string callType[3] = { "Beginner", "Intermediate", "Advanced" };
// END
What is the syntax for randomizing an array so it will randomly choose between those three using the rand fucntion?
ANY HELP?
Feb 14, 2009 at 10:16pm UTC
rand()%n
returns a random integer between 0 and n
Feb 14, 2009 at 10:29pm UTC
ok so you are saying that i cant use it with the array of strings?
if thats true then how would i randomize those strings
Feb 14, 2009 at 10:33pm UTC
array[n]
returns the (n-1)th element of the array...
Feb 14, 2009 at 10:46pm UTC
i'm still new to this language so you are really not helping me by what you are saying, and i know the return values
if you are not going to answer my question then don't bother posting on here because you are absolutely no help
thanks
Feb 14, 2009 at 10:53pm UTC
If rand()%n returns a value from 0 to n-1 and an array with n elements gets for subscripting (brackets) a value from 0 to n-1 what does this suggest you?
Feb 14, 2009 at 11:11pm UTC
alright i get what you are saying, but not fully. its the syntax that is throwing me for a loop so to speak.
Feb 14, 2009 at 11:23pm UTC
Try coding a bit, if you don't get anything working post your code and I'll show what you should modify
Feb 14, 2009 at 11:43pm UTC
how about this
string name[10] = {"Eric", "Tim", "Timmy's Man Friend Steven", "Jermiah", "Jason", "Chris", "Don", "Cheryl", "Blake", "Trevor"};
_name = name[rand() % (10)];
it works for me. does it look good
Feb 14, 2009 at 11:50pm UTC
Yes, it's right.
You don't need parentheses around 10
when posting code use the [ code][ /code] tags so it looks better