javarandom

Java random number with given length


I need to genarate a random number with exactly 6 digits in Java. I know i could loop 6 times over a randomizer but is there a nother way to do this in the standard Java SE?


Solution

  • Generate a number in the range from 100000 to 999999.

    // pseudo code
    int n = 100000 + random_float() * 900000;
    

    For more details see the documentation for Random