-5
我试图从一个数组中使用随机数生成器和名称数组来返回一个随机名。有人可以修复有问题的东西,或者为我提供一个更简单的方法来做到这一点?试图从字符串数组中返回一个名称
Person类:
#include <iostream>
#include <string>
using namespace std;
string Person::generateRandomName()
{
string name[] = { "Robert", "James", "Rachel", "Eve", "Brandon", "Adam", "Ashley", "Brittany" };
int choose = getRandomInt(0, 7);
for (int i = 0; i < 8; i++)
{
if (choose == i)
{
return name[i];
}
}
}
的getRandomInt(最小值,最大值)是另一个类:
#include <iostream>
#include <random>
using namespace std;
int getRandomInt(int min, int max)
{
mt19937 rng;
rng.seed(random_device()());
uniform_int_distribution<mt19937::result_type> random(min, max);
return random(rng);
}
为什么不返回'name [i]'而不是通过for循环? – therainmaker
此外,我的水晶球此刻不工作。你能告诉我你的错误吗? – therainmaker
但是,你有什么困扰? –