You do this by:
- Generating a random index into the array (in your case between 0 and 2); let that index be
i
.
- Emitting
rpsoptions[i]
.
As @ThomasSablik notes, the first step is covered by this question:
How to generate a random number in C++?
Combining the two steps, here is what you could get for your program:
#include <random>
#include <iostream>
int main()
{
std::random_device dev;
std::mt19937 randomness_generator(dev());
std::uniform_int_distribution<std::size_t> index_distribution(0,2);
std::string rpsoptions[3] = {"rock", "paper", "scissors"};
auto i = index_distribution(randomness_generator);
std::cout << rpsoptions[i] << std::endl;
}
Note that I've glossed over the issue of seeding the pseudo-random number generator; that's covered in the first answer at the link above, and would result in a bunch more code. It's important when you actually want to rely on properties of your random distribution, but not so important to illustrate the way you use the (pseudo-)randomly-generated index.