Generating a Random Pin of 5 Digits

I would suggest to generate random number using SecureRandom class and then as it needs to be of 5 digits create a random number between 0 and 99999 using random.nextInt(100000) , here 0 is inclusive and 100000 is exclusive and then format it into 5 digit by appending zero.

SecureRandom random = new SecureRandom();
int num = random.nextInt(100000);
String formatted = String.format("%05d", num); 
System.out.println(formatted);

I hope this solves your problem

Edit: This post incorrectly said 10,000, has been edited to say 100,000

Tags:

Java