Random string function?

Hello I want to know if there is a way to print out a random string that was declared within an array so that when you run the program it'll print out a random string that was in the array. All help is appreciated and welcome. Read the code if you don't get what I'm talking about. Thank you.

1
2
3
string example [3] {"This is an example", "lolol", "Hello friend"};

//How would I print out a random string on the screen? I know the rand() function but I understand it is only for numbers. Thank you for your time. 
you can use rand() to generate a random number between 0 and the amount of elements in the array, to use as a index for accessing a random string in the array. For example:

1
2
3
4
string example [3] {"This is an example", "lolol", "Hello friend"};
srand(time(NULL));
int randStr = rand() % 3
cout << example[randStr];
Last edited on
Thank you so much I appreciate it. Never thought I would get a reply this fast much appreciated. I'll look forward to your answers in the future.
Topic archived. No new replies allowed.