5

Basically, let's say I have an int array that can hold 10 numbers. Which mean I can store 0-9 in each of the index (each number only once).

If I run the code below:

int[] num = new int[10];
for(int i=0;i<10;i++){
    num[i]=i;
}

my array would look like this:

[0],[1],.....,[8],[9]

But how do I randomize the number assignment each time I run the code? For example, I want the array to look something like:

[8],[1],[0].....[6],[3]
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

10

Make it a List<Integer> instead of an array, and use Collections.shuffle() to shuffle it. You can build the int[] from the List after shuffling.

If you really want to do the shuffle directly, search for "Fisher-Yates Shuffle".

Here is an example of using the List technique:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
  public static void main(String args[]) {
    List<Integer> dataList = new ArrayList<Integer>();
    for (int i = 0; i < 10; i++) {
      dataList.add(i);
    }
    Collections.shuffle(dataList);
    int[] num = new int[dataList.size()];
    for (int i = 0; i < dataList.size(); i++) {
      num[i] = dataList.get(i);
    }

    for (int i = 0; i < num.length; i++) {
      System.out.println(num[i]);
    }
  }
}
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • 1
    A List can store any reference type, including Integer. – Patricia Shanahan Mar 04 '13 at 07:22
  • When i typed num.add(1) on eclipse. It says "The method add(String) in the type List is not applicable for the arguments (int)". 'num' is my list variable. –  Mar 04 '13 at 07:26
  • And when I declared it as List, it asks me to remove the part. –  Mar 04 '13 at 07:30
  • What Java version are you using? – Patricia Shanahan Mar 04 '13 at 07:32
  • I've added a complete worked example using List. However, if you prefer to avoid List until you have learned more about it, see my comment about [Fisher-Yates Shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). – Patricia Shanahan Mar 04 '13 at 07:35
  • Your solution look elegant and I can understand it. But When I try to run it I get "The type List is not generic; it cannot be parameterized with arguments " as an error. Could it be the java version? (1.7 in my case) –  Mar 04 '13 at 07:38
  • I changed List to ArrayList and it worked. Thank you so much for the code. –  Mar 04 '13 at 07:49
  • 1
    Be careful which List class you are importing. It must be java.util.List. – Patricia Shanahan Mar 04 '13 at 07:52
1

Collections class has an efficient method for shuffling:

private static Random random;

/**
 * Code from method java.util.Collections.shuffle();
 */
public static void shuffle(int[] array) {
    if (random == null) random = new Random();
    int count = array.length;
    for (int i = count; i > 1; i--) {
        swap(array, i - 1, random.nextInt(i));
    }
}

private static void swap(int[] array, int i, int j) {
    int temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}
KitKat
  • 1,495
  • 14
  • 15