0

I'm trying to create a list of 20 integers between 0 and 26 (so in the 1-25 range) that does not repeat as a part of an assignment. I thought I had it figured out, but the program keeps looping over and over without ever ending. Can anyone help me out?

import java.util.Random;

public class prog433a
{
    public static void main(String args[])
    {
            Random r = new Random();

            int[] list = new int[20];

            for (int k = 0; k < list.length; k++)
            {
                boolean notADupe = false;

                while (notADupe == false)
                {
                    list[k] = r.nextInt(25) + 1;
                    for (int j = 0; j < list.length; j++)
                    {
                        if (list[j] == list [k] && j != k)
                        {
                            notADupe = true;
                        }
                        else
                        {
                            notADupe = false;
                            break;
                        }

                    }

                System.out.println(list[k]);
            }
        }
    }
}

EDIT: This is different from the other question because I am trying to figure out how to check for uniqueness using the methods that I am allowed to use in my assignment (essentially, the ones I'm already using in the code).

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
nccows
  • 107
  • 1
  • 2
  • 10
  • 1
    Although this is a well-known requirement and there are many other questions about it, I think it is not correct to close this one as a duplicate because it primarily asks about the posted code and how to fix it. OP is doing an assignment, not looking for a solution to the generic problem. – Marko Topolnik Feb 24 '15 at 17:28
  • I had, in fact, already looked at the other question and it did not help me figure out my problem. – nccows Feb 24 '15 at 17:30
  • 2
    For your own brain sanity, I would avoid using negative variable names like `notADupe`. it's much harder to understand `while (notADupe == false)` than `while (dupe)`. BTW, `while (notADupe == false)` should be written `while (!notADupe)`, which reads as "while not not a dupe". – JB Nizet Feb 24 '15 at 17:35

1 Answers1

1

I think you've reversed the condition out there. Inside if, you should set notADup to false, rather than true. However, I would make the variable isDup instead, and change the while loop accordingly.

One more suggestion: instead of while (notADupe == false), you should just use while (!notADupe). Never compare boolean variables like that. It might surprise you at times.

So to solve your issue, just change your if-else block to:

if (list[j] == list [k] && j != k) {
    notADupe = false;
    break;
} else {
    notADupe = true;
}

BTW, your solution is a bit complex. For every element, you are iterating over whole array to find duplicate. Rather I would suggest you to maintain a Set<Integer> storing the already seen numbers, and check in that every randomly generated number. If present, skip it and re-generate.

Pseudo code would look something like this:

arr = []  // Your list array, initialize to size 20
seen = [] // A Set
for i from 1 -> arr.length
    num = rand.nextInt(25) + 1
    while seen contains num
        num = rand.nextInt(25) + 1
    seen.add(num)
    arr[i] = num
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525