Skip to main content
added 667 characters in body
Source Link
Florian F
  • 591
  • 4
  • 9

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));
    }

}

I am surprised nobody stated the obvious:

    // safe swap
    static void s(int i, int j){
        if( i!=j ) swap(i,j);
    }

    static void shuffle(){
        s(1,rand(0,1));
        s(2,rand(0,2));
        s(3,rand(0,3));
        s(4,rand(0,4));
        s(5,rand(0,5));
        s(6,rand(0,6));
        s(7,rand(0,7));
        s(8,rand(0,8));
        s(9,rand(0,9));
        s(10,rand(0,10));
        s(11,rand(0,11));
        s(12,rand(0,12));
        s(13,rand(0,13));
        s(14,rand(0,14));
        s(15,rand(0,15));
        s(16,rand(0,16));
        s(17,rand(0,17));
        s(18,rand(0,18));
        s(19,rand(0,19));
        s(20,rand(0,20));
        s(21,rand(0,21));
        s(22,rand(0,22));
        s(23,rand(0,23));
        s(24,rand(0,24));
        s(25,rand(0,25));
        s(26,rand(0,26));
        s(27,rand(0,27));
        s(28,rand(0,28));
        s(29,rand(0,29));
        s(30,rand(0,30));
        s(31,rand(0,31));
        s(32,rand(0,32));
        s(33,rand(0,33));
        s(34,rand(0,34));
        s(35,rand(0,35));
        s(36,rand(0,36));
        s(37,rand(0,37));
        s(38,rand(0,38));
        s(39,rand(0,39));
        s(40,rand(0,40));
        s(41,rand(0,41));
        s(42,rand(0,42));
        s(43,rand(0,43));
        s(44,rand(0,44));
        s(45,rand(0,45));
        s(46,rand(0,46));
        s(47,rand(0,47));
        s(48,rand(0,48));
        s(49,rand(0,49));
        s(50,rand(0,50));
        s(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){
        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;
            s(i,rand(0,i));
        }
    }
    
    static void s(int i, int j){
        if( i!=j ) swap(i,j);
    }

    static void shuffle(){
        while( deck[0]!=0 ) s(0,deck[0]);
        for( deck[0]=52; deck[0]-->1 ; ) s(deck[0],rand(deck[0],51));
        s(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));
    }

}

I am surprised nobody stated the obvious: (I'll assume swap(x,x) does nothing.

    static void shuffle(){
        swap(1,rand(0,1));
        swap(2,rand(0,2));
        swap(3,rand(0,3));
        swap(4,rand(0,4));
        swap(5,rand(0,5));
        swap(6,rand(0,6));
        swap(7,rand(0,7));
        swap(8,rand(0,8));
        swap(9,rand(0,9));
        swap(10,rand(0,10));
        swap(11,rand(0,11));
        swap(12,rand(0,12));
        swap(13,rand(0,13));
        swap(14,rand(0,14));
        swap(15,rand(0,15));
        swap(16,rand(0,16));
        swap(17,rand(0,17));
        swap(18,rand(0,18));
        swap(19,rand(0,19));
        swap(20,rand(0,20));
        swap(21,rand(0,21));
        swap(22,rand(0,22));
        swap(23,rand(0,23));
        swap(24,rand(0,24));
        swap(25,rand(0,25));
        swap(26,rand(0,26));
        swap(27,rand(0,27));
        swap(28,rand(0,28));
        swap(29,rand(0,29));
        swap(30,rand(0,30));
        swap(31,rand(0,31));
        swap(32,rand(0,32));
        swap(33,rand(0,33));
        swap(34,rand(0,34));
        swap(35,rand(0,35));
        swap(36,rand(0,36));
        swap(37,rand(0,37));
        swap(38,rand(0,38));
        swap(39,rand(0,39));
        swap(40,rand(0,40));
        swap(41,rand(0,41));
        swap(42,rand(0,42));
        swap(43,rand(0,43));
        swap(44,rand(0,44));
        swap(45,rand(0,45));
        swap(46,rand(0,46));
        swap(47,rand(0,47));
        swap(48,rand(0,48));
        swap(49,rand(0,49));
        swap(50,rand(0,50));
        swap(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;
            swap(i,rand(0,i));
        }
    }
    
    static void shuffle(){
        while( deck[0]!=0 ) swap(0,deck[0]);
        for( deck[0]=52; deck[0]-->1 ; ) swap(deck[0],rand(deck[0],51));
        swap(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));
    }

}
added 667 characters in body
Source Link
Florian F
  • 591
  • 4
  • 9

I am surprised nobody stated the obvious: (I'll assume swap(x,x) does nothing)

    // safe swap
    static void shuffles(int i, int j){
        if( i!=j ) swap(i,j);
    }

    static void shuffle(){
        s(1,rand(0,1));
    swap    s(2,rand(0,2));
    swap    s(3,rand(0,3));
    swap    s(4,rand(0,4));
    swap    s(5,rand(0,5));
    swap    s(6,rand(0,6));
    swap    s(7,rand(0,7));
    swap    s(8,rand(0,8));
    swap    s(9,rand(0,9));
    swap    s(10,rand(0,10));
    swap    s(11,rand(0,11));
    swap    s(12,rand(0,12));
    swap    s(13,rand(0,13));
    swap    s(14,rand(0,14));
    swap    s(15,rand(0,15));
    swap    s(16,rand(0,16));
    swap    s(17,rand(0,17));
    swap    s(18,rand(0,18));
    swap    s(19,rand(0,19));
    swap    s(20,rand(0,20));
    swap    s(21,rand(0,21));
    swap    s(22,rand(0,22));
    swap    s(23,rand(0,23));
    swap    s(24,rand(0,24));
    swap    s(25,rand(0,25));
    swap    s(26,rand(0,26));
    swap    s(27,rand(0,27));
    swap    s(28,rand(0,28));
    swap    s(29,rand(0,29));
    swap    s(30,rand(0,30));
    swap    s(31,rand(0,31));
    swap    s(32,rand(0,32));
    swap    s(33,rand(0,33));
    swap    s(34,rand(0,34));
    swap    s(35,rand(0,35));
    swap    s(36,rand(0,36));
    swap    s(37,rand(0,37));
    swap    s(38,rand(0,38));
    swap    s(39,rand(0,39));
    swap    s(40,rand(0,40));
    swap    s(41,rand(0,41));
    swap    s(42,rand(0,42));
    swap    s(43,rand(0,43));
    swap    s(44,rand(0,44));
    swap    s(45,rand(0,45));
    swap    s(46,rand(0,46));
    swap    s(47,rand(0,47));
    swap    s(48,rand(0,48));
    swap    s(49,rand(0,49));
    swap    s(50,rand(0,50));
    swap    s(51,rand(0,51));
    }
    package stackexchange;

import java.util.Arrays;

public class ShuffleDryShuffleDry1
{
    static int[] deck = new int[52];
    
    static void swap(int i, int j){
        intdeck[i] t=deck[i];^= deck[j];
        deck[i]deck[j] =^= deck[j];deck[i];
        deck[j]deck[i] =^= t;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;
            swaps(i,rand(0,i));
        }
    }
    
    static void s(int i, int j){
        if( i!=j ) swap(i,j);
    }

    static void shuffle(){
        while( deck[0]!=0 ) swaps(0,deck[0]);
        for( deck[0]=52; deck[0]-->1 ; ) swaps(deck[0],rand(deck[0],51));
        swaps(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));
    }

}

PS: oops, the XOR swap algorithm won't like it. I'll correct that shortly.

I am surprised nobody stated the obvious: (I'll assume swap(x,x) does nothing)

static void shuffle(){
    swap(1,rand(0,1));
    swap(2,rand(0,2));
    swap(3,rand(0,3));
    swap(4,rand(0,4));
    swap(5,rand(0,5));
    swap(6,rand(0,6));
    swap(7,rand(0,7));
    swap(8,rand(0,8));
    swap(9,rand(0,9));
    swap(10,rand(0,10));
    swap(11,rand(0,11));
    swap(12,rand(0,12));
    swap(13,rand(0,13));
    swap(14,rand(0,14));
    swap(15,rand(0,15));
    swap(16,rand(0,16));
    swap(17,rand(0,17));
    swap(18,rand(0,18));
    swap(19,rand(0,19));
    swap(20,rand(0,20));
    swap(21,rand(0,21));
    swap(22,rand(0,22));
    swap(23,rand(0,23));
    swap(24,rand(0,24));
    swap(25,rand(0,25));
    swap(26,rand(0,26));
    swap(27,rand(0,27));
    swap(28,rand(0,28));
    swap(29,rand(0,29));
    swap(30,rand(0,30));
    swap(31,rand(0,31));
    swap(32,rand(0,32));
    swap(33,rand(0,33));
    swap(34,rand(0,34));
    swap(35,rand(0,35));
    swap(36,rand(0,36));
    swap(37,rand(0,37));
    swap(38,rand(0,38));
    swap(39,rand(0,39));
    swap(40,rand(0,40));
    swap(41,rand(0,41));
    swap(42,rand(0,42));
    swap(43,rand(0,43));
    swap(44,rand(0,44));
    swap(45,rand(0,45));
    swap(46,rand(0,46));
    swap(47,rand(0,47));
    swap(48,rand(0,48));
    swap(49,rand(0,49));
    swap(50,rand(0,50));
    swap(51,rand(0,51));
}
package stackexchange;

import java.util.Arrays;

public class ShuffleDry
{
    static int[] deck = new int[52];
    
    static void swap(int i, int j){
        int t=deck[i];
        deck[i] = deck[j];
        deck[j] = t;
    }

    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;
            swap(i,rand(0,i));
        }
    }
    
    static void shuffle(){
        while( deck[0]!=0 ) swap(0,deck[0]);
        for( deck[0]=52; deck[0]-->1 ; ) swap(deck[0],rand(deck[0],51));
        swap(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));
    }

}

PS: oops, the XOR swap algorithm won't like it. I'll correct that shortly.

I am surprised nobody stated the obvious:

    // safe swap
    static void s(int i, int j){
        if( i!=j ) swap(i,j);
    }

    static void shuffle(){
        s(1,rand(0,1));
        s(2,rand(0,2));
        s(3,rand(0,3));
        s(4,rand(0,4));
        s(5,rand(0,5));
        s(6,rand(0,6));
        s(7,rand(0,7));
        s(8,rand(0,8));
        s(9,rand(0,9));
        s(10,rand(0,10));
        s(11,rand(0,11));
        s(12,rand(0,12));
        s(13,rand(0,13));
        s(14,rand(0,14));
        s(15,rand(0,15));
        s(16,rand(0,16));
        s(17,rand(0,17));
        s(18,rand(0,18));
        s(19,rand(0,19));
        s(20,rand(0,20));
        s(21,rand(0,21));
        s(22,rand(0,22));
        s(23,rand(0,23));
        s(24,rand(0,24));
        s(25,rand(0,25));
        s(26,rand(0,26));
        s(27,rand(0,27));
        s(28,rand(0,28));
        s(29,rand(0,29));
        s(30,rand(0,30));
        s(31,rand(0,31));
        s(32,rand(0,32));
        s(33,rand(0,33));
        s(34,rand(0,34));
        s(35,rand(0,35));
        s(36,rand(0,36));
        s(37,rand(0,37));
        s(38,rand(0,38));
        s(39,rand(0,39));
        s(40,rand(0,40));
        s(41,rand(0,41));
        s(42,rand(0,42));
        s(43,rand(0,43));
        s(44,rand(0,44));
        s(45,rand(0,45));
        s(46,rand(0,46));
        s(47,rand(0,47));
        s(48,rand(0,48));
        s(49,rand(0,49));
        s(50,rand(0,50));
        s(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){
        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;
            s(i,rand(0,i));
        }
    }
    
    static void s(int i, int j){
        if( i!=j ) swap(i,j);
    }

    static void shuffle(){
        while( deck[0]!=0 ) s(0,deck[0]);
        for( deck[0]=52; deck[0]-->1 ; ) s(deck[0],rand(deck[0],51));
        s(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));
    }

}
added 667 characters in body
Source Link
Florian F
  • 591
  • 4
  • 9

PS: oops, the XOR swap algorithm won't like it. I'll correct that shortly.

PS: oops, the XOR swap algorithm won't like it. I'll correct that shortly.

added 667 characters in body
Source Link
Florian F
  • 591
  • 4
  • 9
Loading
Source Link
Florian F
  • 591
  • 4
  • 9
Loading