I’m a little stuck with a project I have. I’m supposed to take a list of words using char *, have the user enter how many words they want, randomize that number of words and then output to a text file.
Essentially I would want it to do this:
- Read list of words
- How many words do you want? (ex: 50)
- Take (50) words randomly from that list
- Output to text file
My list is something like this:
char * words [] =
{
"Pistachio",
"Avocado",
"Salami",
"Bologna",
};
And the list goes on….
I’ve used this to determine the number of elements in the array:
int array_size = sizeof ( words ) / sizeof ( words [0] );
and applying it to this:
cout << words[rand()%26] << endl;
I already have it set so that the user can tell the program how many words they want but I can’t get it to choose that number and apply it to the list. Everything I’ve tried so far as either ended up in only one random word output to text. I’m really bad at using the random function, I don’t think I’m using it properly at all! Any help would be greatly appreciated.
You can use the srand and rand function to generater number.
1. set the seed by srand(time(NULL));
2. get the random number = rand%50+1 (max count).