0

I'm currently trying to get my java program to not print out duplicate laws in print out. The software is random picking to print out 1 to 3 laws but I don't want duplicates. I'm still kind of new to java and only have written a few programs so far. This is the most complicated one I've written so far.

Right now I want the software to run the random again if it finds a duplicate till there are no duplicates in the print. Is there anyway to do do this ?

/**
    *Seeds is a random number to pick which if statement will be used. 
    * telling the software how many laws to use during its random 
    * generation process
    * 
    */
    Random seeds = new Random(); 
    seed = seeds.nextInt(3) + 1; 

    if (seed == 1){


        /**Beginning of random law selection for universe. Must find way 
         *Must find way to get rid of duplicate answers 
         */
        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index]);
    }

    else if (seed == 2) {

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index] + "," + Laws[index2]);

    }

    else {

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            int index3 = random.nextInt(Laws.length);
            System.out.println("Law: " + Laws[index] + "," + Laws[index2] + 
                    "," + Laws[index3]);

    }
1
  • less line of code, less problems, 3 declarations of the same String array is not good, declare it once at the top above all if else blocks Commented Mar 27, 2016 at 9:40

2 Answers 2

2

Another way to think of this problem is; you want to select values in a random order. This way you won't get duplicates unless an element appears more than once.

List<String> laws = Arrays.asList("Standard Physics", "Magic", "Mad Science", 
        "Psionics", "Substandard Physics","Exotic");
Collections.shuffle(laws);
// select as many as you need
List<String> lawsSelected = laws.subList(0, 3);
Sign up to request clarification or add additional context in comments.

Comments

0

Here is one approach. You've 3 if conditions so take 3 boolean variables and associate one with each if and make it true when that if is executed.

Random seeds = new Random(); 
    seed = seeds.nextInt(3) + 1; 


boolean selected[] = new boolean[3];
selected[0] = selected[1] = selected[2] = false;//initially none of the if is executed

    if (seed == 1 && !selected[seed-1]){//checking if not previously selected and "seed-1" since array indexes are from 0 to n-1


        /**Beginning of random law selection for universe. Must find way 
         *Must find way to get rid of duplicate answers 
         */
        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index]);
            selected[seed-1] = true;
    }

    else if (seed == 2 && !selected[seed-1]) {

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index] + "," + Laws[index2]);
            selected[seed-1] = true;
    }

    else if (!selected[seed-1]){

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            int index3 = random.nextInt(Laws.length);
            System.out.println("Law: " + Laws[index] + "," + Laws[index2] + 
                    "," + Laws[index3]);
        selected[seed-1] = true;
    }

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.