Generate 16 Digit Random Number in Java

How-to basics · Updated November 23, 2026

To generate a 16-digit random number in Java, use ThreadLocalRandom.current().nextLong(1_000_000_000_000_000L, 10_000_000_000_000_000L), which returns a long between 10^15 (inclusive) and 10^16 (exclusive), guaranteeing exactly 16 digits. For a string with leading zeros allowed, use String.format("%016d", ThreadLocalRandom.current().nextLong(1_000_000_000_000_000L)).

Why 16 Digits Is a Special Case

Java’s primitive integer types have fixed ranges. An int holds up to about 2.1 billion (10 digits), so it cannot represent a 16-digit number. You must use a long, which goes up to 9,223,372,036,854,775,807 (19 digits). However, Math.random() returns a double between 0.0 and 1.0. Multiplying by 10^16 gives a value up to 10^16, but converting that to a long may produce a number with fewer than 16 digits (e.g., 123456789012345). To guarantee exactly 16 digits, you need to bound the range so the smallest possible value is 10^15 (1 followed by 15 zeros).

Using ThreadLocalRandom (Recommended)

The simplest and most efficient method for most applications is ThreadLocalRandom, introduced in Java 7. It avoids the overhead of creating a new Random object each call and is thread-safe. Here is a complete example:

import java.util.concurrent.ThreadLocalRandom;

public class Random16 {
    public static void main(String[] args) {
        long number = ThreadLocalRandom.current().nextLong(1_000_000_000_000_000L, 10_000_000_000_000_000L);
        System.out.println(number);
    }
}

This prints a random 16-digit number. The lower bound is 10^15 (1,000,000,000,000,000) and the upper bound is 10^16 (10,000,000,000,000,000) exclusive, so the result is always between 1,000,000,000,000,000 and 9,999,999,999,999,999.

Using Random and nextLong with Bounds

If you prefer the classic java.util.Random, you can use the nextLong(long bound) method (available since Java 17) or the older nextLong() with a modulus. Here is the Java 17+ approach:

import java.util.Random;

Random rand = new Random();
long number = rand.nextLong(1_000_000_000_000_000L, 10_000_000_000_000_000L);

For older Java versions, you can use Math.abs(rand.nextLong()) % 9_000_000_000_000_000L + 1_000_000_000_000_000L, but be careful: Math.abs(Long.MIN_VALUE) stays negative. A safer formula is rand.nextLong() % 9_000_000_000_000_000L + 1_000_000_000_000_000L, which works because the remainder is non-negative when the divisor is positive (though the distribution is slightly skewed).

Generating a 16-Digit String (Including Leading Zeros)

Sometimes you need a 16-character string that may include leading zeros, such as an ID or a code. In that case, generate a number from 0 to 10^16 – 1 and format it with leading zeros:

import java.util.concurrent.ThreadLocalRandom;

long number = ThreadLocalRandom.current().nextLong(10_000_000_000_000_000L); // 0 to 9,999,999,999,999,999
String formatted = String.format("%016d", number);
System.out.println(formatted);

This always produces a 16-character string, for example 0000000000000123 or 1234567890123456. If you need exactly 16 digits with no leading zeros, use the bounded approach from the first section.

Security: When to Use SecureRandom

If the random number is used for security-sensitive purposes—like generating a password reset token, a cryptographic key, or a lottery draw—you must use SecureRandom instead of Random or ThreadLocalRandom. The latter two use a linear congruential formula that is predictable if an attacker can observe enough outputs. SecureRandom uses a cryptographically strong random number generator (e.g., based on /dev/urandom on Linux). Example:

import java.security.SecureRandom;

SecureRandom secureRandom = new SecureRandom();
long number = secureRandom.nextLong(1_000_000_000_000_000L, 10_000_000_000_000_000L);

Note: SecureRandom has a nextLong(bound) method since Java 17; for earlier versions, use secureRandom.nextLong() % 9_000_000_000_000_000L + 1_000_000_000_000_000L.

Common Pitfalls and How to Avoid Them

  • Using Math.random() and casting to long: This can produce numbers with fewer than 16 digits because the range starts at 0. Always multiply by 10^16 and then add 10^15? Actually, a common mistake is (long)(Math.random() * 10000000000000000L) which gives 0 to 10^16-1, not guaranteed 16 digits. Use a bounded long instead.
  • Overflow with int: Trying to store a 16-digit number in an int causes a compile error or overflow. Always use long.
  • Negative numbers: If you use new Random().nextLong() without bounds, you can get negative values. Always use a bounded method or take the absolute value carefully.
  • Distribution bias: Using % on a range that does not evenly divide 2^64 introduces a slight bias. For most non-security uses, this is negligible, but for simulations, use nextLong(bound) which is unbiased.

Performance and Thread Safety

ThreadLocalRandom is the fastest option for multi-threaded applications because each thread has its own generator, avoiding contention. Random is thread-safe but uses an atomic seed, which can become a bottleneck under high concurrency. SecureRandom is the slowest but necessary for security. For a simple single-threaded program, any of them will work; choose based on your concurrency and security needs.

Alternatives: UUID, BigInteger, and Apache Commons

Another way to get a 16-digit number is to generate a UUID and extract digits, but that is overly complex and not purely random in the same way. You can also use BigInteger with Random:

BigInteger number = new BigInteger(54, new Random()); // 54 bits gives up to ~1.8e16
String formatted = String.format("%016d", number);

But this is less direct. The Apache Commons Lang library has RandomStringUtils.randomNumeric(16), which returns a 16-character numeric string, but it uses Random internally and is not cryptographically secure. For most purposes, the built-in ThreadLocalRandom approach is simplest and fastest.

Frequently Asked Questions

How do I generate a 16-digit number with no leading zeros?

Use ThreadLocalRandom.current().nextLong(1_000_000_000_000_000L, 10_000_000_000_000_000L). This ensures the value is at least 10^15, so it always has exactly 16 digits when printed as a number.

Can I generate a 16-digit number using Math.random()?

Yes, but you must adjust the range. For example, (long)(Math.random() * 9_000_000_000_000_000L) + 1_000_000_000_000_000L gives a 16-digit number. However, this is less efficient and may have a slight bias; ThreadLocalRandom is recommended.

Is SecureRandom necessary for generating random numbers?

Only if the number is used for security, such as passwords, tokens, or encryption keys. For general testing or simulation, Random or ThreadLocalRandom is fine and much faster.

How do I generate a 16-digit number in Java 8?

Java 8 does not have nextLong(bound) on Random or ThreadLocalRandom. Use ThreadLocalRandom.current().nextLong(10_000_000_000_000_000L) for a 0 to 10^16-1 number, or use the modulus trick: ThreadLocalRandom.current().nextLong() % 9_000_000_000_000_000L + 1_000_000_000_000_000L for a guaranteed 16-digit value.

What is the maximum 16-digit number I can generate?

The maximum is 9,999,999,999,999,999 (which is 10^16 – 1). If you generate a number from 0 to 10^16-1, that is the upper bound. If you use the bounded method with upper bound 10^16, the maximum is 9,999,999,999,999,999.

Can I generate a 16-digit number as a string with leading zeros?

Yes, use String.format("%016d", number) after generating a number from 0 to 10^16-1. This pads with zeros to make exactly 16 characters.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.