Question

I know the usual way of shuffling of Array-list; is there another way of mimicking what shuffle does for Array (NOT Lists) ? I already code something that works with rand.NextInt and IF and while; but I highly doubt that is the most efficient way. any Idea considering low resources?

this is what Ive got; but I doubt the efficiency:

       static int[] order = new int[] {1,2,3,4};
       static int[] order2 = new int[] {0,0,0,0,0};
       static int p;
        Random rand = new Random(); 
    int m=global.order[rand.nextInt( global.order.length)];
    global.order2[m]=m;

    int n=global.order[rand.nextInt( global.order.length)];
    while ( m==n )  { n=global.order[rand.nextInt( global.order.length)];}
    global.order2[n]=n;

    int o=global.order[rand.nextInt( global.order.length)];

    while (m==o || n==o) { o=global.order[rand.nextInt( global.order.length)]; }
    global.order2[o]=o;
    for (int y=1; y<=4; y++){
        if (global.order2[y]!=m && global.order2[y]!=n && global.order2[y]!=o){
            global.order2[y]=y;
            global.p=y;
Was it helpful?

Solution

Use Fisher-Yates shuffle (Wikipedia)

You can easily follow this guideline to create your own Java implementation:

  1. Pick a random integer r from the range [0..array.length)
  2. Swap array[0] and array[r]
  3. Pick a new random integer r from the range [1..array.length)
  4. Swap array[1] and array[r]

and so on. This translates fairly easily to a loop. The performance is good, there's no need to reallocate the array or create new objects and execution time scales linearly.

OTHER TIPS

If what you need is an unsorted set with randomly generated values, consider using a HashSet.

To implement this with a normal array your insertions into the array will take O(n) time as you need to look at all the elements to see if it already contains the random number. Using a HashSet insertions will take O(1). You can then convert it to an array.

import java.util.Arrays;
import java.util.HashSet;

public class MakeUnsortedSet {

    public static void main(String[] args) {
        HashSet<Integer> set = new HashSet<>();

        while (set.size() < 10) {
            set.add((int)(Math.random() * 20)); 
        }

        Integer[] ints = new Integer[10];
        set.toArray(ints);

        System.out.println(Arrays.toString(ints));
    }
}

In essence, an ArrayList is just a regular array wraped up in List interface implementation. If you somewhat think you can come up with a better implementation to suit your performence needs, just implement your own List and pass it to Collections.shuffle().

If you want to avoid instantiating a new object of your List implementation for every array, you could implement it in such way you can only replace the reference to the wraped array and make minor modifications.

Anyway, I would first look at ArrayList's source code (and maybe extends it) before trying to reinvent the wheel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top