fork download
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <random>
  6. #include <vector>
  7.  
  8. // Fisher–Yates_shuffle
  9. std::vector<int> FisherYatesShuffle(std::size_t size, std::size_t max_size, std::mt19937& gen)
  10. {
  11. assert(size < max_size);
  12. std::vector<int> res(size);
  13.  
  14. for(std::size_t i = 0; i != max_size; ++i) {
  15. std::uniform_int_distribution<> dis(0, i);
  16. std::size_t j = dis(gen);
  17. if (j < res.size()) {
  18. if (i != j) {
  19. res[i] = res[j];
  20. }
  21. res[j] = 1 + i;
  22. }
  23. }
  24. return res;
  25. }
  26.  
  27. int main()
  28. {
  29. std::random_device rd;
  30. std::mt19937 gen(rd());
  31. std::vector<int> b = FisherYatesShuffle(7, 35, gen);
  32.  
  33. std::copy(b.begin(), b.end(), std::ostream_iterator<int>(std::cout, " "));
  34. return 0;
  35. }
  36.  
Runtime error #stdin #stdout #stderr 0s 3488KB
stdin
Standard input is empty
stdout
25 30 22 2 32 27 19 
stderr
*** Error in `./prog': free(): invalid next size (fast): 0x08d01188 ***
prog: malloc.c:2369: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.