I am creating a text-based game in C++. I am wondering, however, if there is a way to randomize a a response from a set amount of responses.
Say I had a question, and the player answered incorrectly, I want the game to reply with a set response with something along the lines of "Sorry, that is invalid".
However, this does not add much personality to the game, and because the computer in this case is an AI in this particular game, when you type something wrong I am going to have the computer say 'I do not understand', 'What are you talking about', and a few other responses.
Now my question is, how can I make it randomly select a reply out of those responses I have?
Given an array of responses:
int numResponses = 10;
std::string[] responses = { // fill responses }
You can use <random>
, here's setting up your random generator:
std::random_device device;
std::mt19937 generator(device());
std::uniform_int_distribution<> distributor(0, numResponses - 1);
and somewhere in your code:
if(badresponse)
{
int index = distributor(generator);
std::cout << responses[index];
}