Fisher-Yates/Knuth algorithm code example

Example: fisher yates algorithm

import java.util.*;

public class Randomlyefficient{
	public static void main(String[] args)
    {
        Random rand = new Random();
        int[] list = new int[52];
        for ( int  i = 0; i < list.length; i++)
        {
                    list[i] = i;   
        }
       // System.out.println(Arrays.toString(list));
       // System.out.println();

        for ( int i = list.length - 1; i > 0; i--)
        {
            int temp = 0;
            int n = rand.nextInt(i);
            if ( n == i )
            { 
                continue ;
            }
            else 
            { 
                temp = list[n];
                list[n] = list[i];
                list[i] = temp;
            }
        }
        System.out.println(Arrays.toString(list));
		
    }
    /*Random rand = new Random();
            int n = rand.nextInt(52);*/ 
}

Tags:

Misc Example