Dear Experts, I have a vector named Dstr and If I do Dstr.size() it gives me an array. I want to choose a candidate from that Dstr/Dstr.size() randomly. Could you please suggest me how to do that in c++ ?
Thanks in advance Regards
Here's the C++ way of generating a random number. I am assuming that your array/vector is not empty
size_t random(size_t min, size_t max)
{
static std::mt19937 randGen( std::random_device{}() );
return std::uniform_int_distribution<size_t>(min, max)(randGen);
}
auto val = Dstr.at(random(0, Dstr.size() - 1));
auto candidate = Dstr[rand() % Dstr.size()];
std::vectornamedDstr, and doDstr.size()you don't get an "array", you get the number of elements currently in the vector, which is a non-negative integer value.