Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;
import java.util.Random;

public class Main {
    public static ArrayList<Integer> shuffleArray(int size) {
        ArrayList<Integer> shuffled = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            shuffled.add(i);
        }

        Random rnd = new Random();
        for (int i = size - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            // Simple swap
            int a = shuffled.get(index);
            shuffled.set(index, shuffled.get(i));
            shuffled.set(i, a);
        }

        return shuffled;
    }
}