-1

I'm making a rock paper scissors bot that randomly picks rock, paper or scissors. I have an array called rpsoptions as shown below.

#include <iostream>
#include <string>


int main()
{
   std::string rpsoptions[3] = {"rock", "paper", "scissors"};
   std::cout << ( RANDOM ITEM FROM ARRAY) << std::endl;
}

How do I select a random element from the array?

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Smixxand
  • 19
  • 2
  • don't know why this was closed but hey you can use rand and srand for this – JoshKisb Apr 10 '21 at 08:59
  • 1
    Agree, the duplicate is wrong. Am voting to reopen. – Ted Klein Bergman Apr 10 '21 at 08:59
  • 1
    @JoshKisb Please don't recommend `rand` and `srand`: [Why is the use of rand() considered bad?](https://stackoverflow.com/questions/52869166/why-is-the-use-of-rand-considered-bad) You should use the [random library](https://en.cppreference.com/w/cpp/numeric/random) – Thomas Sablik Apr 10 '21 at 08:59
  • oh.. any better alternative – JoshKisb Apr 10 '21 at 09:00
  • It sounds like you're trying to do this in a 'high-level' way and pull the random element out of the array in a single step. Your first step would be to generate a random integer in the range of the number of elements in your array, and then use that randomised index to access the corresponding element in your array. – Username Obfuscation Apr 10 '21 at 09:50

2 Answers2

1

You do this by:

  1. Generating a random index into the array (in your case between 0 and 2); let that index be i.
  2. 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.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
-1

You only need to generate a random number and use it to choose the element into the array:

    int main() {
        srand(time(NULL));
        std::string rpsoptions[3] = {"rock", "paper", "scissors"};
        std::cout << rpsoptions[rand() % 3] << std::endl;
        return 0;
    } 

where rand() % 3 generates a random integer number in [0, 2] range (number of the elements in the array you have).

0xNIC
  • 135
  • 2
  • 10
  • It is generally [not a good idea to use `rand()`](https://stackoverflow.com/questions/52869166/why-is-the-use-of-rand-considered-bad). Its problems do not manifest in your specific example, but if op decides to take up its use, she will likely encounter its deficiencies. – einpoklum Apr 10 '21 at 15:40