I am trying to generate a random 4-digit number using the Java Math.random method. The number must always be 4 digits, i.e., allow for results such as 0001
, or 0023
, or 0123
to be possible. However, the traditional Math.random
method formula below only allows numbers less than 1000 to be 1, 23, or 123.
int i = (int)(Math.random()*10000);
Is there a way to do this using math.random and/or loops? Thank you, in advance, for any suggestions.
I tried researching how to ensure the 4-digit number always has 4 digits, but all results seem to suggest using Random class, or min/max. I DO NOT want to use this random class or max/min code as I have not yet studied it yet.