0

I need to generate a random 2 digit number which will be concatenated to the end of a string. The number MUST be 2 digits (i.e. 00-99, nothing <10 is acceptable).

How can this be done using Java libraries in the simplest way possible?

M-R
  • 411
  • 2
  • 6
  • 15
  • 2
    You probably mean "i.e. 10-99" –  May 04 '16 at 17:11
  • See http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range. Input 10 and 99 as the min and max. – MattCorr May 04 '16 at 17:11
  • `System.out.println(String.format("%02d", 0));` – Elliott Frisch May 04 '16 at 17:18
  • public class MainClass { public static void main(String[] args) { //The String String string ="66"; //Generating Two digits Number that is > 10. Random r = new Random(); int randomNum = r.nextInt(99 - 10) + 10; //Printing the Concatenated Result System.out.println(string+"-"+randomNum); } } – sangeen May 04 '16 at 17:41

1 Answers1

1

A pretty simple way would be to generate two random integers separately, and then add them to the end of your string. That way anything from 1-9 would show up as 01-09.

Using this answer, Generating random integers in a specific range, you generate the two integers. Then create an empty string and add the two variables you used to store your random integers.

Psuedocode:

int a = RANDOMNUMBER
int b = RANDOMNUMBER
String number = "" + a + b
Community
  • 1
  • 1