This question already has an answer here:
- Java generating non-repeating random numbers 6 answers
- Non repeating random integers in Java [duplicate] 4 answers
- Creating random numbers with no duplicates 15 answers
- Generating Unique Random Numbers in Java 12 answers
- How to generate random numbers without repetition 5 answers
I want to know if it's possible, to change the following code so that when I call the method the second time, the randomly generated number won't repeat. Something like if the number before has already been generated, it won't appear anymore.
import java.util.*;
public class Number {
public static void main(String[] args){
Number number = new Number();
number.number();
number.number();
}
public void number(){
Random rand = new Random();
int randomNum = 1 + rand.nextInt((100 - 1) + 1);
System.out.println("The new generated number is: " + x);
}
}
I already looked at some other Stackoverflow questions, but I didn't find anything covering the case above.
Aucun commentaire:
Enregistrer un commentaire