fork(39) download
  1. #include <vector>
  2. #include <algorithm>
  3. #include <cstdlib>
  4. #include <iostream>
  5.  
  6. template<class bidiiter>
  7. bidiiter random_unique(bidiiter begin, bidiiter end, size_t num_random) {
  8. size_t left = std::distance(begin, end);
  9. while (num_random--) {
  10. bidiiter r = begin;
  11. std::advance(r, rand()%left);
  12. std::swap(*begin, *r);
  13. ++begin;
  14. --left;
  15. }
  16. return begin;
  17. }
  18.  
  19. int main() {
  20. std::vector<int> a(10000);
  21. for(int i=0; i<10000; ++i)
  22. a[i] = i;
  23. random_unique(a.begin(), a.end(), 13);
  24. for(int i=0; i<13; ++i) {
  25. std::cout << a[i] << '\n';
  26. }
  27. return 0;
  28. }
Success #stdin #stdout 0.02s 2856KB
stdin
Standard input is empty
stdout
9383
5588
9183
1463
1209
565
7578
6136
4249
3069
8592
7205
9862