fork download
  1. import java.util.Random;
  2. /**
  3.  * Shuffle Deck of Cards
  4.  * @author r.prateek
  5.  */
  6. class ShuffleDeck {
  7. private int[] arr;
  8.  
  9. public ShuffleDeck(int[] input) {
  10. this.arr=input;
  11. }
  12.  
  13. /**
  14. * Shuffle subroutine
  15. */
  16. public void shuffle(){
  17. Random randomGenerator = new Random();
  18. for(int i=0;i<arr.length;i++){
  19. int rand =randomGenerator.nextInt(i+1);
  20. swap(i,rand);
  21. }
  22. System.out.print("After Shuffling: ");
  23. print();
  24. }
  25.  
  26. private void swap(int i,int j){
  27. int temp = arr[i];
  28. arr[i]=arr[j];
  29. arr[j]=temp;
  30. }
  31. /**
  32. * Print Cards
  33. */
  34. public void print(){
  35. for(Integer i:arr)
  36. System.out.print(i + "\t");
  37. }
  38.  
  39. public static void main(String[] args) {
  40. int[] input = {1,2,3,4,5,6,7,8,9,10};
  41.  
  42. ShuffleDeck shuffle= new ShuffleDeck(input);
  43. System.out.print("Before Shuffling: ");
  44. shuffle.print();
  45. System.out.println();
  46. shuffle.shuffle();
  47. }
  48. }
  49.  
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
Before Shuffling: 1	2	3	4	5	6	7	8	9	10	
After Shuffling:  7	4	2	1	6	9	5	10	3	8