How to get rid of repetition of values?

int _tmain(int argc, _TCHAR* argv[]) { srand(time(NULL)); const int n = 100; char *Empty[100]; char *Empty2[100]; char *Names[] = { "Alexandr", "Kostya", "Roman" }; char *Work[] = { "Stoloter", "Posudomoyka", "Shef" }; for (int i = 0; i < 3; i++) { Empty[i] = Names[rand() % 3]; cout << Empty[i] << " "; Empty2[i] = Work[rand() % 3]; cout << Empty2[i] << "\n"; } return 0; } 

It is necessary to choose 3 pairs, but that at the same time there would be no repetition of Names and Professions.

Closed due to the fact that the off-topic participants D-side , Vladimir Martyanov , aleksandr barakin , tutankhamun , AseN 24 Mar '16 at 23:03 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - D-side, aleksandr barakin, tutankhamun
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • From repetitions of what ??? Do you want to randomly select 3 times the name-profession pair? - Alexey Sarovsky
  • Yes, choose 3 pairs, but that at the same time there would be no repetition of Names and Professions. - Alexandr T
  • What is a stolother ? - SergeyA
  • Stolother - the man who washes the table; use strictly on the territory of the hostel. - Alexandr T

3 answers 3

The first thing that occurred to is to write the values ​​of the rand () function into an array and the next time it is called to check whether there is such a value in the array. If there is again calling the rend until it generates non-repeating values ​​(but this variable is suitable if you have Names and Work masses there are not large. In general, since C ++, not C, has STL, it has its own rend, but there are a lot of distributions in it.

  • Could you tell me how to implement this? - Alexandr T

If I truly understand what you want, then the easiest way is to make two arrays with indices and randomly rearrange them.

 int idx1[3] = {0,1,2}; int idx2[3] = {0,1,2}; for(int i = 0; i < 2; ++i) { int j = rand()%(3-i)+i; // Можно использовать функцию swap, если знаете такую... int tmp = idx1[i]; idx1[i] = idx1[j]; idx1[j] = tmp; j = rand()%(3-i)+i; tmp = idx2[i]; idx2[i] = idx2[j]; idx2[j] = tmp; } for (int i = 0; i < 3; i++) { Empty[i] = Names[idx1[i]]; cout << Empty[i] << " "; Empty2[i] = Work[idx2[i]]; cout << Empty2[i] << "\n"; } return 0; 
  • Maybe there is a problem: suddenly there will be not 3 pairs, but more (3x3 = 9 different pairs are possible here) - Alexey Sarovsky
  • The method is extended to any number :) - Harry
  • Thank you, but I would like to use the method I started with. - Alexandr T
  • Yes, please, the decision is yours ... - Harry
  • one
    @AlexandrT, the idea is that you run through the array from the first to the penultimate element ( i is the index of this element) and using rand calculate the index ( j ) in the range from i (inclusive) to the last and exchange the i-th with j-th elements. Cycle to the penultimate, since the latter is no longer with anyone. I can see that this is a generally known algorithm for shuffling ( shuffle ) - avp

As I understand it, the "professions", that the names will always be one number. Then simply mix each array, and then output them.

The function shuffle (> = C ++ 11) or random_shuffle (> = C ++ 98) from the standard library will help. As a generator, use one suitable for you ( http://www.cplusplus.com/reference/random/ , look for the Generators heading).

Code :

 #include <iostream> // std::cout #include <algorithm> // std::shuffle #include <array> // std::array #include <random> // std::default_random_engine #include <chrono> // std::chrono::system_clock using namespace std; int main () { // Help to keep same size using Array = array<string, 3>; Array names = { "Alexandr", "Kostya", "Roman" }; Array work = { "Stoloter", "Posudomoyka", "Shef" }; // obtain a time-based seed: unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // shuffle both arrays shuffle(begin(names), end(names), std::default_random_engine(seed)); // Update seed and shuffle other array seed = chrono::system_clock::now().time_since_epoch().count() + 1; shuffle(begin(work), end(work), std::default_random_engine(seed)); for (size_t idx = 0; idx < names.size(); ++idx) { cout << names[idx] << ' ' << work[idx] << '\n'; } return 0; } 

Exhaust on different starts may be:

 $ ./a.out Kostya Shef Roman Stoloter Alexandr Posudomoyka $ ./a.out Kostya Posudomoyka Roman Stoloter Alexandr Shef $ ./a.out Alexandr Stoloter Kostya Shef Roman Posudomoyka