The problem is that you are not returning the result of a recursive call. Change your code to:
public static int getRandomPINNumber(){
Random randomGenerator = new Random();
int randomNo = randomGenerator.nextInt(10000);
if(randomNo < 1000)
return getRandomPINNumber();
return randomNo;
}
When you call the function for the first time and a number less than 1000 is generated, you call recursively getRandomPINNumber, but ignore the return value.
In addition, you should not call new Random()several times. Call it once and save the result.
source
share