0

I am filling an array with random integers 1 - 10, however while I'm doing this I need to check to see if the Integer is already in the array. I have no problem filling the array with the integers but my code to check for duplicate integers is not working properly.

  package arrayPackage;
  import java.util.Arrays;
  import java.util.Random;
  public class BruteForce 
  {
//declare an integer array with 10 numbers.
int[] array = new int[10];
Random randomInt = new Random();    //random number generator


public void shuffle()
{ 
    //int prevInt=0;
    for(int i = 0; i < array.length; i++)   //for each integer in the array pick a random integer and place in array
    {
        int temp = 1 + randomInt.nextInt(array.length);
        for(int j = i; j < array.length; j++)
        {
            if(temp != array[j] && temp!= array[i])
            {
                array[i] = temp; 
            }
        }

    }
}

public void displayArray()
{
    System.out.println(Arrays.toString(array));
}

}

son2323
  • 5
  • 1
  • 4
  • 1
    Wouldn't it be easier to fill the array with numbers 1-10, then shuffle it? – Kayaman Oct 23 '14 at 06:52
  • You fill in the array with `temp` when there is no duplicate, but where do you handle if there is duplicate? – Pham Trung Oct 23 '14 at 06:55
  • implement the [fisher-yates-knuth shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) it is efficient and unbiased, else use a ready-made implementation (built-in method in java) – Nikos M. Aug 21 '15 at 11:33

5 Answers5

1

seems that you want an array with 1,2,3,...,10 random distributed.

The following solution is better since it only needs exactly 10 random calls:

int array = new int[10];

for (int i = 0; i < 10; i++)
{
  array[i] = i + 1;
}

for (int i = 0; i < 10; i++)
{
  int to = randomInt.nextInt(array.length - i) + i; // get random swap target between i and 9
  swap(array[i], array[to]); // I assume Java has swap function
}
Van Yu
  • 129
  • 1
  • 11
  • Java doesn't have a `swap`-method, so you have to make a simple one yourself: `private void swap(int[] array, int i, int to){ int temp = array[to]; array[to] = array[i]; array[i] = temp; }` or with some checks: `private void swap(int[] array, int i, int to){ if(array != null && array.length > 2 && i < array.length && to < array.length && i != to){ int temp = array[to]; array[to] = array[i]; array[i] = temp; } }` – Kevin Cruijssen Oct 23 '14 at 07:36
1

If your goal is to have an int-array with the numbers 1 to 10, and then shuffle them, try this:

private static int[] array = new int[10];

public static void main(String[] args){     
    // Fill the array
    for(int i = 0; i < array.length; i++)
        array[i] = i + 1;

    shuffle(array);
    System.out.println(Arrays.toString(array));
}

private static void shuffle(int[] array){
    if(array != null && array.length > 2){
        Random randomGenerator = new Random();
        for(int i = array.length - 1; i > 0; i--){
            int randomIndex = randomGenerator.nextInt(i + 1);
            // swap
            int a = array[randomIndex];
            array[randomIndex] = array[i];
            array[i] = a;
        }
    }
}

If your goal is to have an int-array and every time you want to add a random int check wether or not it doesn't exist yet, try using this algorithm to get a random int with excludes:

public int getRandomWithExclusion(Random rnd, int start, int end, int... exclude) {
    int random = start + rnd.nextInt(end - start + 1 - exclude.length);
    for (int ex : exclude) {
        if (random < ex) 
            break;

        random++;
    }
    return random;
}

For information on how to use it, see this SO answer here. The only issue is with the excludes-array, which should change size every time so it only contains the excluded ints, instead of also the default 0 when you create an int-array of size 10. Otherwise exclude.length always returns 10, making the algorithm useless.

Community
  • 1
  • 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • and if one wants to shuffle only part of an array (either with included or excluded items) one can use [a variation of fisher-yates-knuth shuffle](http://stackoverflow.com/questions/26718843/php-shuffle-only-part-of-an-array/28491007#28491007) to do it efficiently – Nikos M. Aug 21 '15 at 11:37
1

It is very easy to write a flawed shuffle algorithm, that is why there exists a good shuffler built in:

http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List)

weston
  • 54,145
  • 21
  • 145
  • 203
0

I've found this that is probably the solution you are searching for..

function shuffle(array) {
  var copy = [], n = array.length, i;

  // While there remain elements to shuffle…
  while (n) {

    // Pick a remaining element…
    i = Math.floor(Math.random() * array.length);

    // If not already shuffled, move it to the new array.
    if (i in array) {
      copy.push(array[i]);
      delete array[i];
      n--;
    }
  }

  return copy;
}

Usage:

public void displayArray()
{
  var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  shuffle(arr);
  System.out.println(Arrays.toString(arr));
}
Gianluca Colombo
  • 717
  • 17
  • 38
0

Why dont you use HashSet<Integer> if you dont want duplicate elements in the list.

Later you convert them to array on your wish.

Rookie007
  • 1,229
  • 2
  • 18
  • 50