Create a 16 digit random number generator in Java

... by Bittle in Programming Help October 19, 2018

Problem:

Create a 16 digit random number generator in Java

Solution:

This can be easily achieved by using ThreadLocalRandom by declaring the bounds equal to the smallest and largest 16 digit numbers.

private static void random() {
    /* return a random long of 16 length */
    long smallest = 1000_0000_0000_0000L;
    long biggest =  9999_9999_9999_9999L;

    // return a long between smallest and biggest (+1 to include biggest as well with the upper bound)
    long random = ThreadLocalRandom.current().nextLong(smallest, biggest+1);
    System.out.println(random);
}

Comments (0)

Search Here