fork download
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4. class ShuffleIndicies {
  5. public static void main (String[] args) {
  6. int[] indicies = range(52);
  7. print("Before:", indicies);
  8. shuffleArray(indicies);
  9. print("After:", indicies);
  10. }
  11.  
  12. // Fisher–Yates shuffle
  13. public static void shuffleArray(int[] ar) {
  14. Random r = new Random();
  15. for (int i = ar.length - 1; i > 0; i--) {
  16. int index = r.nextInt(i + 1);
  17. int t = ar[index];
  18. ar[index] = ar[i];
  19. ar[i] = t;
  20. }
  21. }
  22.  
  23. public static final int[] range(int length) {
  24. return range(0, length);
  25. }
  26.  
  27. public static final int[] range(int start, int length) {
  28. int[] range = new int[length - start + 1];
  29. for (int i = start; i <= length; i++) {
  30. range[i - start] = i;
  31. }
  32. return range;
  33. }
  34.  
  35. public static void print(String label, int[] arr) {
  36. System.out.printf("%s\n%s\n", label, Arrays.toString(arr));
  37. }
  38. }
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
Before:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]
After:
[4, 2, 20, 19, 9, 5, 28, 7, 36, 25, 48, 43, 38, 11, 47, 41, 0, 33, 31, 16, 21, 40, 17, 27, 52, 15, 44, 34, 6, 14, 22, 37, 32, 39, 50, 26, 10, 35, 46, 13, 24, 3, 23, 45, 12, 8, 42, 18, 1, 29, 49, 30, 51]