fork(2) download
  1. #include <algorithm>
  2. #include <iterator>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. int main () {
  8. std::srand(std::time(NULL)); // initialize random seed
  9.  
  10. // shuffle a 2D array
  11. int arr[3][3] = {
  12. {0, 1, 2},
  13. {3, 4, 5},
  14. {6, 7, 8}
  15. };
  16.  
  17. // Shuffle from the first member to the last member.
  18. // The array is interpreted as a 9 element 1D array.
  19. std::random_shuffle(&arr[0][0], &arr[2][3]);
  20.  
  21. // print the result
  22. for (int row = 0; row < 3; ++row) {
  23. for (int col = 0; col < 3; ++col) {
  24. std::cout << arr[row][col] << ' ';
  25. }
  26. std::cout << std::endl;
  27. }
  28. return 0;
  29. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
7 2 1 
5 8 6 
4 0 3