Generating a Random Number between 1 and 10 Java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Generating a random integer between 1 and 10 in Java is simple once you remember the range rules. The main source of bugs is off-by-one thinking, because Java APIs usually define random integer ranges as inclusive of the lower bound and exclusive of the upper bound.
Use ThreadLocalRandom for Most Modern Code
For everyday Java code, ThreadLocalRandom is the clearest answer.
This returns a value from 1 through 10 because the upper bound 11 is exclusive.
That exclusive upper-bound rule is the detail people most often miss.
Use Random if You Already Have One
java.util.Random also works fine.
Here is the logic:
- '
nextInt(10)gives0through9' - adding
1shifts the range to1through10
This is still a correct and common approach.
Math.random() Also Works
If you want a one-liner and do not need a reusable random generator object, Math.random() is acceptable.
The same range logic applies:
- '
Math.random()gives adoublein[0.0, 1.0)' - multiplying by
10gives[0.0, 10.0) - casting to
inttruncates to0through9 - adding
1gives1through10
This works, but many developers prefer ThreadLocalRandom or Random because the intent is clearer.
Know When You Need Secure Randomness
If the number is for a game, simulation, or basic app logic, the normal random APIs are fine. If the number affects security, token generation, or secrets, use SecureRandom instead.
This is slower but appropriate for security-sensitive randomness.
Test the Range, Not Just One Run
Random code often looks correct even when the bounds are wrong. A quick loop makes mistakes obvious.
If you ever see 0 or 11, the bounds are wrong.
Reproducibility for Tests
If you are writing tests or examples and want repeatable output, use a seeded Random instance instead of relying on a fresh generator each run. That gives deterministic sequences for debugging while preserving the same range logic.
This is helpful for unit tests, but not appropriate when the point is real unpredictability.
Common Pitfalls
- Forgetting that many Java random methods use an exclusive upper bound leads to off-by-one errors.
- Writing
nextInt(1, 10)when you really want1through10excludes10because the upper bound is not included. - Using
Math.random()without understanding the cast-and-shift logic can accidentally produce the wrong range. - Reaching for
SecureRandomfor ordinary non-security cases adds overhead without benefit. - Assuming one printed result proves the bounds are correct can hide mistakes that only show up across repeated runs.
Summary
- The easiest modern answer is
ThreadLocalRandom.current().nextInt(1, 11). - With
Random, usenextInt(10) + 1. - With
Math.random(), use(int)(Math.random() * 10) + 1. - Remember that Java random upper bounds are usually exclusive.
- Use
SecureRandomonly when the randomness is security-sensitive.
Related reading
- Generating All Combinations of List n Levels Deep in Java
- Generating random date in a specific range in JAVA
- generating Variations without repetitions / Permutations in java
- Generic return type upper bound - interface vs. class - surprisingly valid code
- Get a BigInteger attribute from Cassandra ResultSet
- Get a list of all threads currently running in Java
- Get a list of all threads currently running in Java
- Get a list of resources from classpath directory

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.