Module 13: Java Math & RandomBeginner20 min read
Math Class, Random & SecureRandom
Perform mathematical operations with Math.pow, Math.sqrt, Math.abs, rounding methods, and generate pseudo-random and cryptographically secure numbers.
Key Takeaways & Core Concepts
- `java.util.Random`: Uses a 48-bit seed and a Linear Congruential Generator (LCG). It is fast and deterministic (given the same seed, it reproduces the exact same sequence—great for unit tests and simulations), but predictable. An attacker observing a few outputs can deduce the seed and predict future numbers.
- `java.security.SecureRandom`: Gathers entropy from the OS kernel (
/dev/urandomor Windows CryptGenRandom). Always useSecureRandomfor OTPs, password reset tokens, API keys, and cryptographic salts.
Introduction
java.lang.Math provides fast, static mathematical operations without needing to instantiate any object—backed by hardware floating-point instructions where supported—while Random and SecureRandom handle pseudo-random and cryptographic random number generation.
Math Geometry & SecureRandom OTP
1import java.security.SecureRandom;
2
3public class Main {
4 public static void main(String[] args) {
5 double radius = 5.0;
6 double area = Math.PI * Math.pow(radius, 2);
7 SecureRandom rng = new SecureRandom();
8 int otp = 100000 + rng.nextInt(900000);
9
10 System.out.printf("Area: %.2f | Hypotenuse(3,4): %.1f%n", area, Math.hypot(3, 4));
11 System.out.println("6-digit OTP valid? " + (otp >= 100000 && otp <= 999999));
12 }
13}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
13 lines
Note: `Math.hypot(3, 4)` computes `sqrt(3^2 + 4^2)` without intermediate overflow.
Essential Math Methods & Precision
| Method | Description | Example |
|---|---|---|
Math.abs(x) | Absolute value | Math.abs(-42) == 42 |
Math.max(a, b) / Math.min(a, b) | Larger / smaller value | Math.max(10, 25) == 25 |
Math.pow(a, b) / Math.sqrt(x) | Power & square root | Math.sqrt(81) == 9.0 |
Math.round(x), ceil, floor | Rounding operations | Math.ceil(4.2) == 5.0 |
Math.addExact(a, b) | Throws ArithmeticException on overflow | Prevents silent int wrap-around |
Random vs SecureRandom
- `java.util.Random`: Uses a 48-bit seed and a Linear Congruential Generator (LCG). It is fast and deterministic (given the same seed, it reproduces the exact same sequence—great for unit tests and simulations), but predictable. An attacker observing a few outputs can deduce the seed and predict future numbers.
- `java.security.SecureRandom`: Gathers entropy from the OS kernel (
/dev/urandomor Windows CryptGenRandom). Always useSecureRandomfor OTPs, password reset tokens, API keys, and cryptographic salts.
Common Mistakes
- Using
Math.random()for security tokens or OTPs—Math.random()delegates tojava.util.Random, which is not cryptographically secure. - Forgetting integer overflow in
Math.abs(Integer.MIN_VALUE), which returnsInteger.MIN_VALUEbecause+2,147,483,648cannot fit in a 32-bit signedint.
Practice & AssessmentJava Programming Tutorial
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)