Is this possible? (Random number generator for strings instead)

1
2
3
4
5
string Random [] = { "Jon", "Chuck", "Joe"};
    std::default_random_engine generator;
	std::uniform_int_distribution<string> distribution("Jon","Joe");
	string random = distribution(generator);
	system ("pause");


So I followed the tutorial on using the random header file, and instead of using it for numbers I wanted to use it with a string. The code above outputs this error:

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\random(2784): error C2440: 'type cast' : cannot convert from 'std::string' to '_Wty'

Is there some other way I can do this while still using the generator found in the random header file?
You're close. You just need to use the int to index a lookup table. The int is randomly generated... not the string:

1
2
3
4
5
6
7
static const std::string Names[] = {"Jon", "Chuck", "Joe"};
static const int size = 3;  // or calculate the size with some kind of 'array_size' function -- another topic

std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0,size-1);

std::string random = Names[ distribution(generator) ];
Topic archived. No new replies allowed.