Question: > 
> **Possible Duplicate:**  
> 
> [Java random number with given length](https://stackoverflow.com/questions/5392693/java-random-number-with-given-length) 
> 
> 
> 

I have been trying to generate a 15 digit long number in java but it seems I have failed to do it so far as using:

This is producing a maximum of 10 digits. 

```
  Random random = new Random();
  int rand15Digt = random.nextInt(15);

```

How can I generate it successfully?

Answer: 
Use [Random](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html)'s method `public long nextLong()`
Any number can be formatted into 15 decimal digits when presented as a string. This is achieved when the number is converted to a String, e.g.:

```
System.out.println(String.format("%015d", 1));

// prints: 000000000000001

```

If you want to generate a random number that lies between `100,000,000,000,000` and `999,999,999,999,999` then you can [perform a trick](https://stackoverflow.com/a/5392715/474189) such as:

```
Random random = new Random();    
long n = (long) (100000000000000L + random.nextFloat() * 900000000000000L);

```

If your ultimate goal is to have a 15-character string containing random decimal digits, and you're happy with third-party libraries, consider Apache commons [`RandomStringUtils`](http://commons.apache.org/lang/api/index.html?org/apache/commons/lang3/RandomStringUtils.html):

```
boolean useLetters = false;
boolean useNumbers = true;
int stringLength = 15;

String result = RandomStringUtils.random(stringLength, useLetters, useNumbers) 

```