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

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Souvik Maity
  • 21
  • 1
  • 3
  • 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? – t.niese Nov 10 '19 at 19:52
  • 1
    check out this answer https://stackoverflow.com/questions/8861568/fisher-yates-variation it is the classical way to shuffle a list. – AndersK Nov 10 '19 at 19:52
  • Please read [ask] with a [mcve]. Here is a good place to start https://en.cppreference.com/w/cpp/header/random – Richard Critten Nov 10 '19 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. – Some programmer dude Nov 10 '19 at 19:54

3 Answers3

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.

Evg
  • 25,259
  • 5
  • 41
  • 83
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));
Midnight Exigent
  • 615
  • 4
  • 15
-1

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

  • I downvoted because in C++ you should not use the C random library, but the c++ random library. – bolov Nov 10 '19 at 20:14
  • Can you give me please the counter part in C++ ? I would be glad to see it ! – Sébastien Bémelmans Nov 10 '19 at 20:34
  • 1
    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) – Jerry Jeremiah Nov 10 '19 at 20:44
  • 1
    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: https://stackoverflow.com/questions/52869166/why-is-the-use-of-rand-considered-bad , https://stackoverflow.com/questions/39288595/why-not-just-use-random-device , https://stackoverflow.com/a/58775301/2805305 . – bolov Nov 10 '19 at 22:54
  • 1
    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 – bolov Nov 10 '19 at 23:09
  • He asks a simple question, I gave him a simple solution... I know how to use random in c++, but as we can see now, solutions in c++ are really long and maybe he doesn't need them for now. – Sébastien Bémelmans Nov 11 '19 at 08:08