vendredi 17 avril 2020

C++ how to choose a random variable from a vector list?

Im trying to create a function that randomly selects a customers Name from a vector List. Below is my attempt at this function using the mt19937 engine

Carly:Cat:ABCCCCE.
Dodgy Dan:Dog:BCACECC.
Ernie:Ettin:AABCCDD.
Sally:Snake:AEEEEEE.

for example, above is the contents of the vector list. If working the function has a possibility to return either Carly, Dodgy Dan, Ernine or Sally randomly(the customers name).

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;
}

An expected output could include

Carly was randomlly selected
Dodgy Dan was randomlly selected 



Aucun commentaire:

Enregistrer un commentaire