Question
I want to generate a random number within a specific range in Android or Java, for example between 65 and 80.
I tried this code:
Random r = new Random();
int i1 = r.nextInt(80) + 65;
However, this is not giving the correct result. It can return values greater than the maximum value of 80.
How can I correctly generate a random integer between a minimum and maximum value?
Short Answer
By the end of this page, you will understand how to generate random integers inside a specific range in Java for Android, why nextInt() works the way it does, how to build the correct formula using a minimum and maximum value, and which common mistakes cause numbers to go outside the expected range.
Concept
In Java, Random.nextInt(bound) does not generate a number from 1 to bound. It generates a number from:
0 to bound - 1
So this code:
r.nextInt(80)
returns a value from 0 to 79.
If you then add 65:
r.nextInt(80) + 65
the final result becomes:
65 to 144
That is why your code can return values greater than 80.
To generate a random number between min and max inclusive, use this formula:
Mental Model
Think of nextInt(bound) as a machine that always starts counting from 0.
If you want numbers in a different range, you do two things:
- Tell it how many numbers you need
- Shift the result to where your range starts
For the range 65 to 80:
- The machine should produce
16possible numbers - Those numbers are initially
0to15 - Then you shift them by adding
65 - Final result becomes
65to80
So the process is:
0..15 -> add 65 -> 65..80
That is the key idea: generate from zero, then shift into place.
Syntax and Examples
The standard Java syntax is:
Random random = new Random();
int value = random.nextInt(max - min + 1) + min;
Example: random number from 65 to 80
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
int value = random.nextInt(80 - 65 + 1) + 65;
System.out.println(value);
}
}
Possible output:
65
72
80
68
Example using variables
Step by Step Execution
Consider this code:
Random random = new Random();
int min = 65;
int max = 80;
int value = random.nextInt(max - min + 1) + min;
Let’s trace it step by step.
Step 1: Calculate the range size
max - min + 1
becomes:
80 - 65 + 1 = 16
So now the code is effectively:
int value = random.nextInt(16) + 65;
Step 2: nextInt(16) generates a value from 0 to 15
Possible results:
Real World Use Cases
Random numbers in a specific range are common in real apps.
Games
- rolling dice
- generating enemy damage values
- random spawn positions within limits
int damage = random.nextInt(10 - 5 + 1) + 5;
OTP or verification demo values
- generating a 4-digit or 6-digit demo code
int code = random.nextInt(9999 - 1000 + 1) + 1000;
Quiz or flashcard apps
- selecting a random question index
- picking random rewards
Test data generation
- random ages, scores, ratings, or IDs for development
int age = random.nextInt(60 - 18 + 1) + 18;
UI behavior
Real Codebase Usage
In real projects, developers often wrap random-range logic inside a helper method so they do not repeat the formula everywhere.
Helper method
import java.util.Random;
public class RandomUtils {
private static final Random random = new Random();
public static int randomInRange(int min, int max) {
if (min > max) {
throw new IllegalArgumentException("min cannot be greater than max");
}
return random.nextInt(max - min + 1) + min;
}
}
Usage:
int value = RandomUtils.randomInRange(65, 80);
Guard clause for validation
A common pattern is to validate inputs early:
Common Mistakes
1. Using the max value directly in nextInt()
Broken code:
Random random = new Random();
int value = random.nextInt(80) + 65;
Problem:
- this produces
65to144 80is treated as the count, not the final maximum
Fix:
int value = random.nextInt(80 - 65 + 1) + 65;
2. Forgetting the + 1
Broken code:
int value = random.nextInt(max - min) + min;
Problem:
Comparisons
| Approach | Example | Result Range | Includes Max? | Notes |
|---|---|---|---|---|
| Incorrect direct bound | random.nextInt(80) + 65 | 65..144 | No intended max control | Wrong for custom range |
| Correct inclusive range | random.nextInt(max - min + 1) + min | min..max | Yes | Most common solution |
| Exclusive upper bound style | random.nextInt(max - min) + min | min..max-1 | No | Useful when upper bound should be excluded |
vs
Cheat Sheet
Generate random integer in an inclusive range
int value = random.nextInt(max - min + 1) + min;
Example
Random random = new Random();
int value = random.nextInt(80 - 65 + 1) + 65;
What nextInt(bound) means
random.nextInt(5)
returns:
0, 1, 2, 3, 4
Rules
nextInt(bound)starts at0- upper limit is excluded
- use
max - min + 1for inclusive ranges
FAQ
How do I generate a random number between 65 and 80 in Java?
Use:
Random random = new Random();
int value = random.nextInt(80 - 65 + 1) + 65;
Why does random.nextInt(80) + 65 go above 80?
Because nextInt(80) returns 0 to 79. After adding 65, the result becomes 65 to 144.
Does nextInt(bound) include the bound value?
No. It includes 0 and excludes bound.
How do I include both minimum and maximum values?
Use:
random.nextInt(max - min + 1) + min
Mini Project
Description
Build a small Java utility that simulates rolling a custom numbered token. The user provides a minimum and maximum value, and the program generates a random number inside that range. This demonstrates correct random-range logic and simple input validation.
Goal
Create a reusable method that returns a random integer between a given minimum and maximum value, inclusive.
Requirements
- Create a method that accepts
minandmaxas parameters. - Validate that
minis not greater thanmax. - Return a random integer within the inclusive range.
- Call the method multiple times to show different results.
- Use Java syntax compatible with Android projects.
Keep learning
Related questions
Accessing Kotlin Extension Functions from Java
Learn how Kotlin extension functions are compiled and how to call them correctly from Java with clear examples and common pitfalls.
Android AlarmManager Example: Scheduling Tasks with AlarmManager
Learn how to use Android AlarmManager to schedule tasks, set alarms, and handle broadcasts with a simple beginner example.
Android Foreground Service Notification Channels in Kotlin
Learn why startForeground fails on Android 8.1 and how to create a valid notification channel for foreground services in Kotlin.