I need to create algorithm implementation in C++ to generate random numbers to f.e table without repeat and list.
I created something code like that but it\'s stop w
I just want to state some other solutions to the following problems, which may work faster (or with some shorter code) (in some cases).
Imagine the case where you need all the elements of the set {0, 1, .., n - 1}, probability of choosing the last non-chosen element(in last iteration) is equal to 1/n, expected value is then n iterations for picking only one element.
vector p;
for(int i = 0; i < n; i++) p.push_back(i);
random_shuffle(p.begin(), p.end();
and then you can take first K elements of vector p (or whole vector, if you need the whole permutation).