2

I want to fill an array of size X with random integers from 0 to X with no duplicates. The catch is I must only use arrays to store the collections of int, no ArrayLists. How do I go about implementing this?

I don't understand why I can't seem to get this. But this is my most recent bit of code that fills the list but allows for duplicates.

System.out.print("Zero up to but excluding ");
int limit = scanner.nextInt();

// create index the size of the limit
int [] index = new int[limit];

for(int fill=0;fill<limit;fill+=1){
    index[fill] = (limit);
}

int randomNumber = 0;
Random rand = new Random();
int [] randoms = new int[limit];
boolean flag = true;

// CODE TO NOT PRINT DOUBLES
for (int z=0;z<limit;z+=1){
    randomNumber = rand.nextInt(limit);
    int i=0;
    while (i<limit){
        if (index[i] == randomNumber){
            flag = true;
        }
        else {
            flag = false;
            break;
        }
        i+=1;
    }
    if (flag == false){
        randoms[z] = randomNumber;
        index[z] = randomNumber;
    }
}
System.out.println("Randoms: "+java.util.Arrays.toString(randoms));
2
  • 1
    The standard way to do this is to use an ArrayList, fill it with the numbers 0..X-1, then shuffle it. So, with arrays, do the same. Commented Jul 23, 2016 at 22:56
  • Related : Generating an array of unique random doubles Commented Jul 23, 2016 at 22:58

4 Answers 4

6

Here's one way to do it:

  1. Create an array of length N
  2. Fill it from 0 to N-1
  3. Run a for loop and swap randomly 2 indices

Code:

// Step 1
int N = 10;
int[] array = new int[N];

// Step 2
for(int i=0; i < N; i++)
   array[i] = i;

// Step 3
for(int i=0; i < N; i++) {
   int randIndex = (int) (Math.random() * N);
   int tmp = array[i];
   array[i] = array[randIndex];
   array[randIndex] = tmp;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Why not rephrase the problem to shuffling an array of integers. First fill the array monotonically with the numbers 0 to X. Then use the Random() function to select one of the X numbers to exchange with the number in position 0. Repeat as many times as you may like. Done.

Comments

0

Here is your bug:

  while (i<limit){
    if (index[i] == randomNumber){
      flag = true;
    }
    else {flag = false;break;}  <--- rest of the array is skipped
    i+=1;
  }

after you generated a new number, you start to check for equality , however once you find that randomNumber!=index[i] (else statement) you break out of the while. look this: actual array is 3,4,5,1 your new number is 5, you compare it to 3 just to find out that they different so flag is set to false and break out happens.

1 Comment

remove your break and put it after flag=true if (index[i] == randomNumber){ flag = true;break; } else { flag = false; }
0

Consider using another array filled with elements in order from 0 to X. Then, with this array, shuffle the elements around. How do you go about this? Use a loop to traverse through every single element of the array, and for each iteration, choose a random number from 0 to array.length - 1 and switch the elements at the index you're currently on and the random index. This is how it would look like,

In your main, you would have an array initialized by doing this,

int[] arr = new int[10];//10 can be interchangeable with any other number
for(int i = 0; i < arr.length; i++){
    arr[i] = i;
}
shuffleArray(arr);

And the shuffle method would look like this,

public int[] shuffleArray(int[] arr){
    Random rand = new Random();
    for(int i = 0; i < arr.length; i++){
        int r = rand.nextInt(arr.length);//generate a random number from 0 to X
        int k = arr[i];
        arr[i] = arr[r];
        arr[r] = k;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.