-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
但是,你有什麼困擾? –