I am surprised nobody stated the obvious: (I'll assume swap(x,x) does nothing.
// safe swap
static void s(int i, int j){
if( i!=j ) swap(i,j);
}
static void shuffle(){
sswap(1,rand(0,1));
sswap(2,rand(0,2));
sswap(3,rand(0,3));
sswap(4,rand(0,4));
sswap(5,rand(0,5));
sswap(6,rand(0,6));
sswap(7,rand(0,7));
sswap(8,rand(0,8));
sswap(9,rand(0,9));
sswap(10,rand(0,10));
sswap(11,rand(0,11));
sswap(12,rand(0,12));
sswap(13,rand(0,13));
sswap(14,rand(0,14));
sswap(15,rand(0,15));
sswap(16,rand(0,16));
sswap(17,rand(0,17));
sswap(18,rand(0,18));
sswap(19,rand(0,19));
sswap(20,rand(0,20));
sswap(21,rand(0,21));
sswap(22,rand(0,22));
sswap(23,rand(0,23));
sswap(24,rand(0,24));
sswap(25,rand(0,25));
sswap(26,rand(0,26));
sswap(27,rand(0,27));
sswap(28,rand(0,28));
sswap(29,rand(0,29));
sswap(30,rand(0,30));
sswap(31,rand(0,31));
sswap(32,rand(0,32));
sswap(33,rand(0,33));
sswap(34,rand(0,34));
sswap(35,rand(0,35));
sswap(36,rand(0,36));
sswap(37,rand(0,37));
sswap(38,rand(0,38));
sswap(39,rand(0,39));
sswap(40,rand(0,40));
sswap(41,rand(0,41));
sswap(42,rand(0,42));
sswap(43,rand(0,43));
sswap(44,rand(0,44));
sswap(45,rand(0,45));
sswap(46,rand(0,46));
sswap(47,rand(0,47));
sswap(48,rand(0,48));
sswap(49,rand(0,49));
sswap(50,rand(0,50));
sswap(51,rand(0,51));
}
package stackexchange;
import java.util.Arrays;
public class ShuffleDry1
{
static int[] deck = new int[52];
static void swap(int i, int j){
if( deck[i]!=deck[j] ){
deck[i] ^= deck[j];
deck[j] ^= deck[i];
deck[i] ^= deck[j];
}
}
static int rand(int min, int max){
return (int)Math.floor(Math.random()*(max-min+1))+min;
}
static void initialize(){
for( int i=0 ; i<deck.length ; i++ ){
deck[i] = i;
sswap(i,rand(0,i));
}
}
static void s(int i, int j){
if( i!=j ) swap(i,j);
}
static void shuffle(){
while( deck[0]!=0 ) sswap(0,deck[0]);
for( deck[0]=52; deck[0]-->1 ; ) sswap(deck[0],rand(deck[0],51));
sswap(0,rand(0,51));
}
public static void main(String[] args) {
initialize();
System.out.println("init: " + Arrays.toString(deck));
shuffle();
System.out.println("rand: " + Arrays.toString(deck));
}
}