Java Array Shuffle shuffle()

Here you can find the source of shuffle()

Description

shuffle

License

Open Source License

Declaration

public static void shuffle() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Random;

public class Main {
    static int no;
    static int py;
    static int os;
    static int dl;
    static int sl;
    static int total = 0;
    private static int[] seq;

    public static void shuffle() {
        //Counters
        int i;//from   w  w  w .j  ava2  s . c om
        int j;
        int tmp;
        int rmd;

        Random rnd = new Random();

        for (i = 0, j = 0; i < no; i++, j++) {
            seq[j] = 0;
        }
        for (i = 0; i < py; i++, j++) {
            seq[j] = 1;
        }
        for (i = 0; i < os; i++, j++) {
            seq[j] = 2;
        }
        for (i = 0; i < dl; i++, j++) {
            seq[j] = 3;
        }
        for (i = 0; i < sl; i++, j++) {
            seq[j] = 4;
        }
        for (i = 0, j = total - 1; j > 0; i++, j--) {

            rmd = rnd.nextInt() % (j + 1);
            if (rmd < 0) {
                rmd = rmd * -1;
            }
            tmp = seq[rmd + i];
            seq[rmd + i] = seq[i];
            seq[i] = tmp;
        }

    }
}

Related

  1. randomize(final T[] array)
  2. shuffle(byte[] array)
  3. shuffle(byte[] values, Random rnd)
  4. shuffle(Collection items, Random random)
  5. shuffle(Collection objects)