tags:

views:

1360

answers:

7

Hi i am trying to generate a 5 digit int array in java and am having trouble on where to start. None of the numbers in the array can be duplicates. i can generate random numbers for it fine but just cant figure out how to compare the numbers to each other and replace any duplicates.

thanks

+10  A: 

You can use a java.util.Set instead of an array as it is guaranteed to have only unique elements.

Rahul
+1  A: 

try this:

int[] digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Random random = new Random();

int[] generateId() {
    int[] clone = digits.clone();
    int[] id = new int[5];

    for (int i = 0; i < 5; i++) {
        int candidate;

        do {
            candidate = random.nextInt(10);
        } while (clone[candidate] == -1);

        id[i] = clone[candidate];
        clone[candidate] = -1;
    }

    return id;
}
dfa
explain downvotes, or they are pointless
dfa
Downvote removed. It's tactical voting that gets on my nerves.
karim79
+2  A: 

You can get rid of the duplicates by converting the array into a TreeSet (that will also sort them):

int numbers[] { 4 5 7 6 5 7 5 89 847 7 94 093 02 10 11 10 11 };
TreeSet set new TreeSet(Arrays.asList(numbers));
for (int no : set)
  System.out.println(no);
karim79
+3  A: 

If I understand you correctly, you want a random 5 digit number, with no digit repeated?

If so, one way is to shuffle a list of the digits 0-9, then pick the first 5 elements.

EDIT

Integer[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Random random = new Random();

public Integer[] generateId() {
    List<Integer> id = Arrays.asList(digits);
    Collections.shuffle(id, random);
    return id.subList(0, 5).toArray(new Integer[0]);
}
Jonathan Maddison
+2  A: 

this generates it in O(number of digits), no inner loops, no shuffling <- this could be expensive if the number of choices gets really big

int[] digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Random random = new Random();

int[] generateId() {
    int[] choices = digits.clone();
    int[] id = new int[5];

    for (int i = 0; i < 5; i++) {
        // one less choice to choose from each time
        int index = random.nextInt(choices.length - i);
        id[i] = choices[index];
        // "remove" used item by replacing it with item at end of range
        // because that index at the end won't be considered in next round
        choices[index] = choices[choices.length - i - 1];
    }

    return id;
}
newacct
+1 this is far more smart than mine
dfa
+1  A: 

As an alternative, just sort the array and iterate it.

    List<int> myList = new List<int>() { 1, 1, 2, 3, 4, 5, 5, 7 , 1, 7};
    myList.Sort();
    for (int i = myList.Count - 1; i > 0; i--)
    {
        if (myList[i] == myList[i - 1])
            myList.RemoveAt(i);
    }

But of course it's best not to get any duplicates to start with.

Carra
A: 

First off i want to thank all of you for helping out i really appreciate it.

I got this program to work the way i want but it seems like there should be an easier way though. Here is what i did. Any more comments would be awesome.

do
 {
  for (int i = 0; i < 5; i++) 
  {
   iNumber = generator.nextInt(9) + 1;
   numbers[i] = iNumber;
  }
 }
 while(numbers[0] == numbers[1] || numbers[0] == numbers[2] || numbers[0] == numbers[3] || numbers[0] == numbers[4] || numbers[1] == numbers[2] || numbers[1] == numbers[3] || numbers[1] == numbers[4] || numbers[2] == numbers[3] || numbers[2] == numbers[4] || numbers[3] == numbers[4]);
John
Look, just generate random numbers and put them in a Set<Integer> while it's size is less or equal to 5. Then call it's toArray method to export them to an aray. Of course, the Array will contain Integers, not ints
Sandman
@sandman: I don't know if that is a good idea, since Set gives no guarantees about the order it returns elements AFAIK. @John: There are at least two cleaner solutions proposed in this thread?
Jonathan Maddison
@sventek: I didn't realize that the numbers have to be ordered? In that case I suggest using a SortedSet. It does make inserts more expensive, but I still believe this is a pretty good solution.
Sandman