Pick random string from Array

#include <iostream>
#include <stdlib.h>
#include <time.h>

int main ()
{
srand ( time(NULL) ); //initialize the random seed


const char arrayNum[4] = {'1', '3', '7', '9'};
int RandIndex = rand() % 4; //generates a random number between 0 and 3
cout << arrayNum[RandIndex];
}
What this code does, is that it randomly genrate a number from arrayNum[4], what i want to know is this:
Instead of 1,3,7,9. where then the randindex obtain one number from the const char array[4] i want strings, basically words like "cake", or "toast"

Is there anyway to do this, is there anyway to write a program that randomly generate strings instead of just of vaules.

A sample code will be nice, along with some explaination

I am really new to this, keep that in mind
Last edited on
yes.

instead of using a const char array, try instead using an array of std::strings.

1
2
3
4
5
6

#include <string>

//....

std::string textArray[4] = {"Cake", "Toast", "Butter", Jelly"}; 


If you are asking about how to generate random sentences, that's a masters thesis right there. Good luck to you with that.
Never thought it was this simple, thought you had to at least use class or something. (probably cause i had the wrong idea or didn't read too carefully)

"If you are asking about how to generate random sentences, that's a masters thesis right there. Good luck to you with that."

intersting
Topic archived. No new replies allowed.