Random String Selection From Library

I would like to use "cout" to display a randomly selected string from a library. For my specific project I would like to randomly generate a first name for a game character.
Say I wanted to choose one of the 3 names: Bob, Mike or Sam. No need for the full code, just an idea of how I could do this.



Last edited on
1
2
string names[] = { "Bob", "Mike", "Sam" };
cout << "Hello " << names[rand()%3] << endl;
1
2
3
4
5
6
auto rand {std::mt19937 {std::random_device {}()}};
auto randNo {std::uniform_int_distribution<size_t> {0, 2}};    // Min, max

const std::string names[] {"Bob", "Mike", "Sam"};

std::cout << "Hello " << names[RandNo(rand)] << endl;

Topic archived. No new replies allowed.