shuffle Array using Random - Android java.lang

Android examples for java.lang:Array Element

Description

shuffle Array using Random

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    public static int[] shuffleArray(int[] ar) {
        Random rnd = new Random();
        for (int i = ar.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            // Simple swap
            int a = ar[index];
            ar[index] = ar[i];// www.  j  av a 2  s  .  co m
            ar[i] = a;
        }

        return ar;
    }
}

Related Tutorials