[!NOTE] Basic operators like
+,-,*,/are built securely into Java. But for advanced tasks like absolute values, square roots, and randomness, you utilize theMathclass.
The Math Class Toolkit
The Math class contains static methods. This means you do not have to create an object via new Math()—you just access the tools directly!
int maxNumber = Math.max(5, 10); // returns 10
int minNumber = Math.min(5, 10); // returns 5
double root = Math.sqrt(64); // returns 8.0
double absolute = Math.abs(-4.7); // returns 4.7
Generating Random Numbers
One of the most heavily used functions in game development and randomized testing is Math.random().
By default, it returns a double value with a positive sign, greater than or equal to 0.0 and strictly less than 1.0.
double randomNum = Math.random(); // E.g., 0.8273648
Controlling the Range
If you want to generate a random integer between 0 and 100, you have to multiply the result and explicitly "cast" the double back down into an int.
// 1. Math.random() generates 0.55
// 2. Multiplied by 101 becomes 55.55
// 3. The (int) cast chops off the decimals, leaving 55.
int randomInt = (int)(Math.random() * 101);
[!TIP] If you need complex randomization (like Gaussian distributions or robust seeds), look into the
java.util.Randomclass instance approach instead ofMath.random().
Where Math Shows Up in Real Programs
The Math class appears in scoring systems, games, analytics, geometry, finance calculations, pagination, and random practice generators. Even simple helper methods like Math.max and Math.min can make code clearer than manual condition checks.
Clamping a Value
Clamping means forcing a value to stay inside a range. This is useful for scores, percentages, ratings, and progress bars.
int score = 118;
int safeScore = Math.max(0, Math.min(score, 100));
System.out.println(safeScore); // 100
Random Number in a Range
int min = 1;
int max = 6;
int dice = min + (int) (Math.random() * (max - min + 1));
System.out.println("Dice: " + dice);
The + 1 is important when you want the maximum value to be possible. Without it, a dice roll from 1 to 6 would never produce 6.
Common Mistakes
- Forgetting that
Math.random()is less than1.0, never exactly1.0. - Using integer division when a decimal answer is needed.
- Expecting
Math.round,Math.floor, andMath.ceilto behave the same.
Mini Practice
Write a program that simulates rolling two dice, prints each dice value, and prints the total. Then clamp a percentage value so it always stays between 0 and 100.
Practice Lab: Dice and Percentage Tools
Use Math methods to solve small practical tasks.
- Generate two random dice values from 1 to 6.
- Print both dice and their total.
- Calculate a percentage from marks obtained and total marks.
- Clamp the percentage between 0 and 100 using
Math.maxandMath.min.
Goal: Practice random ranges, casting, and safe numeric boundaries.
Revision Checkpoint
- Static methods: Call
Mathmethods directly, such asMath.max(). - Random range:
Math.random()returns from0.0inclusive to1.0exclusive. - Casting: Use
(int)carefully when converting decimals to integers. - Clamp: Combine
Math.maxandMath.minto keep values inside bounds. - Rounding: Know the difference between
round,floor, andceil.
Before the quiz: Write the formula for a random dice roll from 1 to 6.