Fisher-Yates shuffle Java implementation This is my attempt at an implementation of the modern Fisher-Yates shuffle in Java. I'm not sure if it can be made more efficient, but I did my best to make it as simple as possible, and I learned how to use generics specifically for this, so If I did something wrong in that regard, let me know.

package com.kestrel.test;

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

public class ShuffleTest
{
    public static &lt;T&gt; ArrayList&lt;T&gt; shuffle(ArrayList&lt;T&gt; a)
    {
        Random rand = new Random();

        for(int n = a.size() - 1; n &gt; 0; n--)
        {
            int index = rand.nextInt(n + 1);
            T temp = a.get(index);

            a.set(index, a.get(n));
            a.set(n, temp);
        }
        return a;
    }

    public static &lt;T&gt; T[] shuffle(T[] a)
    {
        Random rand = new Random();

        for(int n = a.length - 1; n &gt; 0; n--)
        {
            int index = rand.nextInt(n + 1);
            T temp = a[index];

            a[index] = a[n];
            a[n] = temp;
        }
        return a;
    }
}


I know that I would have to implement methods for all of the primitive array types separately, but I'm short on time, and I don't think autoboxing will slow down my code all that much.
You are both mutating &amp; returning the value you are passing to the function.  Pick one.   Either return void or return a new array/ArrayList without mutating the input. 

Your first shuffle() method requires an ArrayList, yet any Collection which implements the List interface would work, and will work well if it has \$O(1)\$ get &amp; set complexity.  So consider loosening the type from a concrete type to the List interface.  (It will even work for LinkedList, albeit with horrible performance, but working slowly is arguably better than not being able to work at all.)



The value returned by ArrayList&lt;T&gt;::set(int idx, T obj) is the previous contents of that location.  Therefor, the temporary is not necessary.

        T temp = a.get(index);
        a.set(index, a.get(n));
        a.set(n, temp);


can become:

        a.set(n, a.set(index, a.get(n)));


or more clearly, just Collections.swap(a, n, index);.

Similarly,

        T temp = a[index];
        a[index] = a[n];
        a[n] = temp;


can also become Collections.swap(a, n, index);, a similar function which takes a T[] instead of a List&lt;?&gt; as the first argument.



Here is an implementation of your "something like K&lt;T&gt; using generics" from the comments.  And no longer coding from the hip, so the Java syntax is actually correct.

MacBook-Pro:~ aneufeld$ jshell
|  Welcome to JShell -- Version 10.0.1
|  For an introduction type: /help intro

jshell&gt; public class ShuffleTest {
   ...&gt;   public static &lt;T,K extends List&lt;T&gt;&gt; K shuffle(Collection&lt;T&gt; a, Supplier&lt;K&gt; supplier) {
   ...&gt;     K dup = supplier.get();
   ...&gt;     dup.addAll(a);
   ...&gt;     Collections.shuffle(dup);  // Or use your shuffle implementation
   ...&gt;     return dup;
   ...&gt;   }
   ...&gt; }
|  created class ShuffleTest

jshell&gt; var orig = List.of("Hello", "world");
orig ==&gt; [Hello, world]

jshell&gt; ArrayList&lt;String&gt; shuffled = ShuffleTest.shuffle(orig, ArrayList&lt;String&gt;::new);
shuffled ==&gt; [world, Hello]

jshell&gt;