Tuesday 15 June 2010

java - Genereating Random Number method -


I need help writing a method that will return a random 4 digit number with no duplication. I'm not allowed to use the string anyhow ...

This is what I have done so far and the row I ran into is getting an error. I ran: solve Can not be done

  public static int generateSecretNumber () {random R = new random (); Int x = r.nextInt (1000) ;; X = x + 1000; Return x;    

line:

  int x = + ran .nextInt (1000);   

should read

  int x = r.nextInt (1000);   

One more thing - you say that you want to generate a random 4 digit number with no repetition. It can take a while because it is perfect for a random number generator, to return the same number several times, in the same way when you flip a coin, you can get 4 heads in a row.

No comments:

Post a Comment