0

I'm trying to get the secret code from 1 - 40, not to repeat any numbers. How am I able to compare each of them and not get any duplicates?

I have extensively looked through Java documentation and asked my lectures and I can't get a working answer. I understand the concept, what I'm meant to do, but just can't get the coding right.

Here is my code:

public static void main(String[] args) {
 int[] secret = new int[5];
 //int[] secret = {0,0,0,0,0};
 int[][] num = new int[3][5];
 int correctL1 = 0;
 int correctL2 = 0;
 int correctL3 = 0;
 for (int i = 0; i<5; i++){ // to get secret numbers.
 secret[i] = (int) ((Math.random() * (40 - 1)) + 1);
 }
 System.out.println(Arrays.toString(secret));
}

I have tried putting this into the loop to get another number but it's still giving me duplicates.

if(((secret[i] == secret[0]) || (secret[i] == secret[1]) || 
 (secret[i] == secret[2]) || (secret[i] == secret[3]) ||
 (secret[i] == secret[4])) || (secret[i] != 0)) {
 secret[i] = ((int) ((Math.random() * (40 - 1)) + 1));
}
Nicolas Filotto
45.2k11 gold badges97 silver badges129 bronze badges
asked Oct 19, 2016 at 12:08
1
  • since Java is an object oriented language you should get used to OO approaches: Java has different Collection types. they roughly split into Lists, which support and order, null elements and duplicates, and Sets, which support uniqueness of elements. So the easieast approach is to collect the input data in an HashSet. Commented Oct 19, 2016 at 12:19

5 Answers 5

3

In Java 8, you can use Random#ints to easily generate an array of distinct random numbers:

int[] secret = new Random().ints(1, 40).distinct().limit(5).toArray();

Ideone Demo

Otherwise, you can generate a Set<Integer> by using a while loop:

Set<Integer> secret = new HashSet<>();
Random gen = new Random();
while (secret.size() < 5) {
 secret.add(gen.nextInt(40 - 1) + 1);
}
answered Oct 19, 2016 at 12:17

Comments

3

Simply use a Set<Integer> to keep the already generated numbers and iterate as long as the generated number is part of the generated numbers.

Set<Integer> existing = new HashSet<>();
for (int i = 0; i<5; i++){ // to get secret numbers.
 // Loop until the generated number is not part of the already generated numbers
 int value;
 do {
 value = (int) ((Math.random() * (40 - 1)) + 1);
 } while (!existing.add(value));
 secret[i] = value;
}
answered Oct 19, 2016 at 12:19

1 Comment

you can simplify with while(existing.size() < 5) { existing.add(X); }
2

Why not creating a Set<Integer> (so you will not get any duplicate) and iterate until its size is 5 ?

answered Oct 19, 2016 at 12:11

Comments

0

Just use Set Collection..for better performance you can use below code

Set<Integer> data=new HashSet<Integer>();

If you want to get all elements by ascending order,you can use TreeSet

OR Other Logic is Every collection has contains method which returns boolean for e.g.data.contains(o)

answered Oct 19, 2016 at 12:32

Comments

0
while(true){
 int[] secret = new int[5];
 for (int i = 0; i < 5; i++) { // to get secret numbers.
 secret[i] = (int) ((Math.random() * (40 - 1)) + 1);
 }
 System.out.println("Array:" + Arrays.toString(secret));
 Set<Integer> mySet = new HashSet<Integer>();
 for (int i = 0; i < 5; i++) { // to get secret numbers.
 mySet.add(secret[i]);
 }
 if (mySet.size() == secret.length) {
 System.out.println("OK");
 break;
 } else {
 System.out.println("Not ok");
 }
 }
answered Oct 19, 2016 at 12:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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