-1

I have looked at a lot of others,i still don't get how to input the non-repeat code into my code below.Newbie here,require someone to explain.

import java.util.Random;

class random{
public static void main(String[] args) {
    Random dice = new Random();
    int number;
    for (int counter = 1; counter <= 20; counter++) {
        number = 1 + dice.nextInt(100);
        System.out.println(number + " ");
    }
}
Shahzeb
  • 4,745
  • 4
  • 27
  • 40
Leon Ku
  • 19
  • 4
  • Hi, welcome to StackOverFlow (SO). Please take a minute to read [how to ask a question](http://stackoverflow.com/help/how-to-ask), and edit your current post based on the specifications referred to in this article. Thanks, and happy coding. – UnknownOctopus Jul 14 '15 at 03:47
  • Good start, but the if number = counter; line isn't Java. Can you add to the post an example of an "other" examples you looked at that you don't understand? It would help us a lot if we see what you are trying to add and your actual attempt with it. As it is, it is really hard to give good advice with what you have posted here. – Guy Schalnat Jul 14 '15 at 03:49
  • Add the generated numbers in a List. Once generated by using Random Generator, if the number already exists, generate another number. Keep on doing this, until you generate a number which does not exist. Then move on to generate next number. This sounds complicated, but you will be surprised that repetition with Random.nextInt() will be less likely. – zookastos Jul 14 '15 at 03:55
  • 1
    possible duplicate of [Creating random numbers with no duplicates](http://stackoverflow.com/questions/4040001/creating-random-numbers-with-no-duplicates) – Madhawa Priyashantha Jul 14 '15 at 03:56

4 Answers4

2

First add the random number to a set, this will stop duplicate entries. Then loop through and print out the set. There are a couple different ways you could do this but here is a way with the least amount of change to your code:

Set<Integer> numbers = new HashSet<Integer>();
Random dice = new Random();
int number;
for (int counter = 1; counter <= 20;) {
    number = 1 + dice.nextInt(100);
    if(numbers.add(number)) { //Only increment the counter if the number wasn't already in the set
        counter++;
    }
}
for(Integer num : numbers) {
    System.out.println(num + " ");
}
MrMadsen
  • 2,713
  • 3
  • 22
  • 31
1

A little bit simpler approach than suggested by MrMadsen is to control the set size:

Set<Integer> numbers = new LinkedHashSet<>();
Random dice = new Random();
while(numbers.size() < 20)
    numbers.add(dice.nextInt(100)+1);
for(Integer num : numbers) {
    System.out.println(num + " ");
}

Just for completeness here's Java-8 solution:

new Random().ints(1, 101).distinct().limit(20).forEach(System.out::println);
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
0

instead of just printing the random number have a set(which allows only unique elements) to capture the number and have a if condition to check the length of the set (set.size() == 20) come out of the loop until then you run the for loop to find the random numbers. I think this will solve your problem

flipper
  • 146
  • 3
  • 11
0

Keep track of what is generated previously. Try this

public static void main(String...args) {
        Random dice = new Random();
        Set<Integer> previouslyGenerated = new  HashSet<Integer>();
        int number = 0,counter = 0;      
        while (counter <20){
            number = 1+dice.nextInt(100);
            if (!previouslyGenerated.contains(number)){
                previouslyGenerated.add(number);
                System.out.println(number + " ");
                counter++;
            }       

        }       

}
Shahzeb
  • 4,745
  • 4
  • 27
  • 40