0

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

4
  • 1
    There are so many questions and answers here on SO about generating a random number in a certain range, and how to choose a random element in a vector. So what is the exact problem you have when you try to apply those methods? Commented Nov 10, 2019 at 19:52
  • 1
    check out this answer stackoverflow.com/questions/8861568/fisher-yates-variation it is the classical way to shuffle a list. Commented Nov 10, 2019 at 19:52
  • Please read How to Ask with a minimal reproducible example. Here is a good place to start en.cppreference.com/w/cpp/header/random Commented Nov 10, 2019 at 19:52
  • 3
    If you have a std::vector named Dstr, and do Dstr.size() you don't get an "array", you get the number of elements currently in the vector, which is a non-negative integer value. Commented Nov 10, 2019 at 19:54

3 Answers 3

3

To get a random element out of your vector, you can use std::sample:

decltype(Dstr)::value_type element;
std::sample(Dstr.begin(), Dstr.end(), &element, 1, std::mt19937{std::random_device{}()});

C++17 is required.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

-1

auto candidate = Dstr[rand() % Dstr.size()];

6 Comments

I downvoted because in C++ you should not use the C random library, but the c++ random library.
Can you give me please the counter part in C++ ? I would be glad to see it !
Maybe as well as downvoting perhaps someone should explain WHY we shouldn't use the C random functions (e.g. because the mod operator makes some results more likely than others unless the size is a power of two)
I believe the best help is the one which nudges you in the right direction, making you do the hard work and research. You didn't know that C random is bad so you needed someone pointing you this. But do you really need someone to type "why is C rand bad" into Google for you? Ok, be it as you want: here it is: stackoverflow.com/questions/52869166/… , stackoverflow.com/questions/39288595/… , stackoverflow.com/a/58775301/2805305 .
Just don't expect to learn the critical skills required to search, analize, filter, select, combine, apply and adapt solutions to your own problem if you don't practice this and instead expect to be fed spooned every solution. I am sorry to be so blunt and abrasive, but you look like you genuinely try to learn so I only tell you this because I hope it can help you and other readers as well. There is value in asking for help and there is value in struggling on your own. Don't take one for granted and don't dismiss the other one
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.