This post is about getting specific number of digits of random numbers in Java. We will be using the Random API from Java for this.

Let’s assume we want 5 random integers from 0 to a 1000. Then, we will use the following code snippet:

String randomNumberString = String.format("%05d", new Random().nextInt(10000));

As it can be seen, we have used String.format to give us the specific number of digits.

Categories: Java

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *