【发布时间】:2020-04-17 07:29:14
【问题描述】:
我正在尝试创建一个从向量列表中随机选择客户姓名的函数。下面是我使用 mt19937 引擎对这个功能的尝试
Carly:Cat:ABCCCCE.
Dodgy Dan:Dog:BCACECC.
Ernie:Ettin:AABCCDD.
Sally:Snake:AEEEEEE.
例如,上面是向量列表的内容。如果使用该功能,可能会随机返回 Carly、Dodgy Dan、Ernine 或 Sally(客户姓名)。
struct Customer {
std::string customerName;
};
float randomCus(const vector<Customer>& customerList,const Builder& b) {
float total;
std::random_device random_device;
std::mt19937 engine{random_device()};
std::uniform_int_distribution<int> dist(0, customerList.size() - 1);
for(Customer x:customerList) {
for(int i=0; i < customerList.size(); i++){
int random_element = x.customerName[dist(engine)];
cout << random_element << "This is a random customer name: ";
}
return total;
}
预期的输出可能包括
Carly was randomlly selected
Dodgy Dan was randomlly selected
【问题讨论】:
-
您正在从每个
customerName中选择一个随机字符。您想选择customerList的随机元素