fork(1) download
  1. import java.io.*;
  2.  
  3. class random
  4. {
  5.  
  6. public static void shuffleArray(int[] cards)
  7. {
  8. int temp, index;int ar[]=new int[cards.length];
  9. int count=0;
  10. for (int i = 0; i < cards.length; i++)
  11. {
  12. index = (int) (Math.random() * (cards.length - i)) + i;
  13. ar[count++]=index;
  14. temp = cards[i];
  15. cards[i] = cards[index];
  16. cards[index] = temp;
  17. }
  18.  
  19. }
  20.  
  21. public static void main(String ar[])throws Exception
  22.  
  23. {
  24. // System.out.println("Enter The Number of Elements in Array");
  25. String n= br.readLine();
  26. int i=Integer.parseInt(n);
  27.  
  28. int a[]=new int[i];
  29. for(int j=0;j<i;j++)
  30. {
  31. String s=br.readLine();
  32. a[j]=Integer.parseInt(s);
  33. }
  34.  
  35. shuffleArray(a);
  36. System.out.println();
  37. for(i=0;i<a.length;i++)
  38. System.out.print(a[i] + " " );
  39. }
  40.  
  41. }
Success #stdin #stdout 0.09s 212416KB
stdin
5
1
2
3
4
5
stdout
1 2 4 5 3