Generate 6 digit random number

Kraken :

I just want to generate 6 digit random number, and the range should be start from 000000 to 999999.

new Random().nextInt(999999) is returning me number but it is not in 6 digit.

Dev Sabby :

Its as simple as that, you can use your code and just do one thing extra here

String.format("%06d", number);

this will return your number in string format, so the "0" will be "000000".

Here is the code.

public static String getRandomNumberString() {
    // It will generate 6 digit random Number.
    // from 0 to 999999
    Random rnd = new Random();
    int number = rnd.nextInt(999999);

    // this will convert any number sequence into 6 character.
    return String.format("%06d", number);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=84619&siteId=1