To generate a random 10-digit number in Java, use long number = (long)(Math.random() * 9_000_000_000L) + 1_000_000_000L; or, for a cryptographically secure option, ThreadLocalRandom.current().nextLong(1_000_000_000L, 10_000_000_000L). The first method returns a number from 1,000,000,000 to 9,999,999,999 (inclusive), while the second returns from 1,000,000,000 to 9,999,999,999 as well, but with better randomness.
Understanding the Range: Why 10 Digits?
A 10-digit number in Java (and most programming languages) is any integer from 1,000,000,000 (1 billion) to 9,999,999,999 (9,999,999,999). Notice that 10,000,000,000 is 11 digits, so it’s excluded. The range has exactly 9 billion possible values. If you need to include leading zeros (e.g., “0123456789”), that’s a different problem—you’d generate a number from 0 to 9,999,999,999 and format it with leading zeros using String.format("%010d", number).
Using Math.random()
The simplest approach uses Math.random(), which returns a double between 0.0 (inclusive) and 1.0 (exclusive). To get a 10-digit number, you multiply by the size of the range (9 billion) and add the minimum (1 billion). Here’s the code:
long number = (long)(Math.random() * 9_000_000_000L) + 1_000_000_000L;
This works because Math.random() * 9_000_000_000L gives a double from 0.0 to 8,999,999,999.999…, casting to long truncates to 0 to 8,999,999,999, and adding 1 billion gives 1,000,000,000 to 9,999,999,999. The cast is necessary because Math.random() returns a double, and you need an integer.
Using ThreadLocalRandom (Recommended)
For most applications, ThreadLocalRandom is better. It’s faster in multi-threaded environments and avoids the potential for collisions when multiple threads generate numbers concurrently. The syntax is:
import java.util.concurrent.ThreadLocalRandom;
long number = ThreadLocalRandom.current().nextLong(1_000_000_000L, 10_000_000_000L);
The nextLong(origin, bound) method returns a value from origin (inclusive) to bound (exclusive). So passing 1,000,000,000 and 10,000,000,000 gives you exactly the 10-digit range. This method is available since Java 1.7 and is the go-to for single-threaded or multi-threaded code.
Using java.util.Random
If you need a reusable random object, use java.util.Random:
import java.util.Random;
Random rand = new Random();
long number = 1_000_000_000L + (long)(rand.nextDouble() * 9_000_000_000L);
Alternatively, you can use nextLong() with a bound, but that’s more complex because nextLong(int bound) doesn’t exist. The above works fine. Note that Random is not thread-safe; if multiple threads share a single Random instance, you need synchronization, which is why ThreadLocalRandom is often preferred.
Cryptographically Secure Random
If you’re generating random numbers for security-sensitive purposes (e.g., tokens, passwords, lottery draws), use SecureRandom:
import java.security.SecureRandom;
SecureRandom secureRandom = new SecureRandom();
long number = 1_000_000_000L + (long)(secureRandom.nextDouble() * 9_000_000_000L);
Or, more efficiently, use nextLong() with a range:
long number = 1_000_000_000L + (long)(secureRandom.nextDouble() * 9_000_000_000L);
SecureRandom uses a cryptographically strong pseudo-random number generator (CSPRNG), making it unpredictable. It’s slower than Random but necessary for security. For non-security use, don’t use it—it’s overkill.
Common Pitfalls and Edge Cases
One common mistake is using int instead of long. A 10-digit number exceeds the maximum int value (2,147,483,647), so you’ll get an overflow and a negative or truncated result. Always use long. Another pitfall is generating a number from 0 to 9,999,999,999 and then adding 1,000,000,000—that gives a range of 1,000,000,000 to 10,999,999,999, which includes 11-digit numbers. Stick to the formula above. Also, be aware that Math.random() and Random.nextDouble() have a 53-bit mantissa, which is enough for 9 billion range, but if you need a uniform distribution over the full 64-bit range, you’d need a different approach.
Performance and Thread Safety
Performance matters when generating millions of numbers. Math.random() is essentially a static Random instance, so it’s synchronized and can be a bottleneck. ThreadLocalRandom is the fastest in multi-threaded contexts because each thread has its own generator. In single-threaded code, new Random() is slightly faster than ThreadLocalRandom due to less overhead, but the difference is negligible. For security, SecureRandom is much slower, but you shouldn’t use it unless necessary.
Alternatives: Third-Party Libraries
If you’re using Apache Commons Math, you can use RandomDataGenerator:
import org.apache.commons.math3.random.RandomDataGenerator;
RandomDataGenerator rdg = new RandomDataGenerator();
long number = rdg.nextLong(1_000_000_000L, 10_000_000_000L - 1);
Note that nextLong(lower, upper) is inclusive of both bounds, so you pass 9_999_999_999L as upper. This is useful if you’re already using Commons Math. However, for most projects, the standard library is sufficient.
Frequently Asked Questions
How do I generate a random 10-digit number with leading zeros?
Generate a number from 0 to 9,999,999,999 using ThreadLocalRandom.current().nextLong(0, 10_000_000_000L), then format with String.format("%010d", number). This pads with zeros to make exactly 10 digits.
Why does my random number sometimes have 9 digits?
Because you’re generating from 0 to 9,999,999,999, which includes numbers below 1 billion. To force 10 digits, start at 1 billion as shown.
Is Math.random() good enough for a lottery?
No. Math.random() is not cryptographically secure; it’s predictable if an attacker knows the seed. Use SecureRandom for any security-sensitive randomness.
Can I use Random.nextInt() to get a 10-digit number?
No, because nextInt() returns at most a 10-digit number only if you call nextInt(9_000_000_000)—but that’s impossible because the bound must be a positive int, and 9 billion exceeds Integer.MAX_VALUE. You must use long.
How do I generate a list of unique 10-digit numbers?
Use a Set to store generated numbers, loop until the set size reaches your desired count. For large counts, consider using a Collections.shuffle on a list of all possible numbers, but that’s memory-intensive (9 billion entries).
What’s the difference between nextLong(origin, bound) and nextLong()?nextLong(origin, bound) returns a number in a specified range, while nextLong() returns any long from -2^63 to 2^63-1. You’d need to manipulate the latter to get a 10-digit range.
Related
- Free Online Data Entry Software for PC
- Generate 3 Random Numbers: How to Do It
- How to Enter Data in RStudio: A Step-by-Step Guide
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Cell Description in Excel: Meaning, Contents, and NotesIn Excel, a cell is one box in a worksheet grid, formed where a row and a column…
- Excel: Add Superscript to the Ribbon Button and Use ItYes. In Excel for Microsoft 365 on Windows, you can add the Superscript command to a custom group…
- How to Record an Entire PowerPoint PresentationTo record an entire presentation in PowerPoint, open it in the desktop app, choose Record > From Beginning,…
- How to Not Copy Hidden Rows in Excel Without ErrorsTo avoid copying hidden rows in Excel, select the range you need, choose Home > Find & Select…
- How to Create Inbox Rules in Outlook on Windows and WebTo create an inbox rule in Outlook, open the rules settings, choose a condition such as who sent…
- Excel Shortcut Desk Mat: What It Is and How to ChooseAn Excel shortcut desk mat is a large mouse-and-keyboard surface printed with keyboard commands for Microsoft Excel. It…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.