selecting strings

I'm trying to make a game.
I want the program to randomly pick from the given strings. How do I do that?

#include <iostream>
#include <string>

using namespace std;

int main()
{

cout << "welcome to MASH!"<< endl;
string Mansion, Apartment,Shack,House;

return 0
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <random>
#include <ctime>

std::string random_string()
{
     static const std::string strings[] = { "Mansion", "Apartment", "Shack", "House", "Cottage", "Villa", "Manor", "Bungalow" } ;
     static constexpr std::size_t NUM_STRINGS = sizeof(strings) / sizeof( strings[0] ) ;

     // http://en.cppreference.com/w/cpp/numeric/random
     static std::mt19937 rng( std::time(nullptr) ) ;
     static std::uniform_int_distribution<std::size_t> distrib( 0, NUM_STRINGS-1 ) ;

     return strings[ distrib(rng) ] ;
}

int main()
{
     for( int i = 0 ; i < 10 ; ++i ) std::cout << i << ". " << random_string() << '\n' ;
}

http://coliru.stacked-crooked.com/a/c830e8f3a2435dd6
What exactly did you do? can you explain it
"Random number engines generate pseudo-random numbers using seed data as entropy source."
"mersenne_twister_engine is a random number engine based on Mersenne Twister algorithm.
It produces high quality unsigned integer random numbers"
static std::mt19937 rng( std::time(nullptr) ) ; // our random number engine (seeded with the current time)

"A random number distribution post-processes the output of an random number engine in such a way that resulting output is distributed according to a defined statistical probability density function."
"std::uniform_int_distribution produces random integer values uniformly distributed on the closed interval [a, b]"
1
2
// our random number distribution (to generate a random integer in the range zero to NUM_STRINGS-1:
static std::uniform_int_distribution<std::size_t> distrib( 0, NUM_STRINGS-1 ) ;


1
2
// generate a random number in the range zero to NUM_STRINGS-1 and return the string at that position
return strings[ distrib(rng) ] ;
Topic archived. No new replies allowed.