dimanche 8 octobre 2017

Java: Random number inside a method, which shouldn't repeat [duplicate]

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