Generate random number between 1 to 5 in Java

By | August 9, 2020

How to generate random number between 1 to 5 in Java:

We can use nextInt() method of Random class to generate random number between 1 to 5 in java.



If we just use nextInt(5), then it will give a random number between 0 to 4. So if you don’t want 0 then you can use nextInt(5)+1 to get a random number between 1 to 5.


Recommended Posts:

How to remove last element from an ArrayList

Remove element from ArrayList

How to swap two numbers without using third variable in Java ***

How to capitalize each word in Java

Java interview questions

Generate random between 1 to 10 in Java


Note: If you like and want to contribute any articles, then please send your articles to 

If you want to improve this article or find any mistake in the article, then please use comment box to share your feedback.

Links: TestNG

arrays tostring in java | selenium interview questions | binary search






Play & Win contest by Selenium99

Home 1

Leave a Reply

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