All pastes #1695089 Raw Edit

PermutationGenerator

public java v1 · immutable
#1695089 ·published 2009-12-01 01:48 UTC
rendered paste body
/** * A class that generates 10 random permutations of the numbers 1-10. */    public class PermutationGenerator    {                public PermutationGenerator()        {        }        /**         * A method to print the contents of array a.         * @param a The array to print.         */        public void printArray(int[] a)         {	for (int k = 0; k < a.length; k++)	    System.out.print("  " + a[k]);              System.out.println();        }     /**     * A method to produce the next random permutation.     */    public int[] nextPermutation()    {	int[] b = (int[])a.clone();	for (int k = b.length - 1; k > 0; k--) 	{	    int w = (int)Math.floor(Math.random() * (k+1)); //Math.random returns a double < 1 and > 0	    int temp = b[w]; //swap contents of array.	    b[w] = b[k];	    b[k] = temp;	}	return b;    }    int[] a = {1,2,3,4,5,6,7,8,9,10};            // print 10 random permutations    public static void main(String[] args)     {        PermutationGenerator pg = new PermutationGenerator();	for (int k = 0; k < 10; k++) 	    {	        int[] b = pg.nextPermutation();	        pg.printArray(b);	    }    }}