Take the 2-minute tour ×

I hav a numeric array,which contains 20 elements.I am displaying the numbers randomly for a blackberry application,bt i want dat all d numbers generated should b unique.It should b randomly generated,bt it has b unique until all the elemnts in the array is exhausted.I am giving the piece of code here,if anyone can help me out,i will b extremely grateful.

static int quesNum[] = new int[20];
static int quesCount = -1;

private static void initialize(){

    Random rgen = new Random();  // Random number generator

    //--- Initialize the array 
    for (int i=0; i<quesNum.length; i++) {
        quesNum[i] = i;

    //--- Shuffle by exchanging each element randomly
        int randomPosition = rgen.nextInt(quesNum.length);

        int temp = quesNum[i];

        quesNum[i] = quesNum[randomPosition];

        quesNum[randomPosition] = temp;


/*Changed the code to get a unique random number

public static int getQuestionNumber() {
    if(quesCount < quesNum.length){
        return quesNum[quesCount];
       quesCount = -1;
       return getQuestionNumber();
share|improve this question
I would really appreciate if you avoided teenager abbreviations (e.g. "b" vs "be"). It makes reading harder. Thank you. –  Vladimir Dyuzhev Jun 20 '10 at 0:25

4 Answers 4

Shuffle first, then iterate:

for(Integer val : listOfValues) {
  // give it to user


Some wording of OP makes me think Collections.shuffle() is not supported on Blackberry. Then advise is to copy the code of Collections.shuffle(List,Random) into the application.

share|improve this answer

What you're describing is a perfect application for just shuffling the array.

share|improve this answer
int len = 20;
Integer[] arr = new Integer[len];
    arr[i] = Integer.valueOf(i+1);

Now the array is shuffled and you can iterate over it.

share|improve this answer

You can use an ArrayList instead of the Array and delete each generated number.

share|improve this answer

Your Answer


