This is a very common scenario in programming when there is a requirement to use some random number. This tutorial will help us to generate it in Java. In this post, we will see “how to create a unique random number in Java?”

There are a couple of ways to generate a unique random number, here I am mentioning two ways to do that.

Varied examples to generate a unique random number in Java...!!! Share on X


If we go by the first way, it works around (0-9) digits to make the unique random number so there is a possibility of re-occurrence of the same number (even the possibility is less as we increase the count of digits that we used to make this number). But, still, conceptually, the probability is always there of repetition of the same number. (but, yes chances are less, so we are good to use it.)

But, if we go by the second way, it works on timestamp basis so it would always be unique. I prefer to use this way.

1st way:

2nd way:

Varied examples to generate a unique random number in Java...!!! Share on X

If you like this post, please click like button and share it with others on Twitter. Also, check out my other useful blog posts on Java:

Other Useful References:


  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks. Verma Deepak