Java
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/urandom or Windows CryptGenRandom). Always use SecureRandom for 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

MethodDescriptionExample
Math.abs(x)Absolute valueMath.abs(-42) == 42
Math.max(a, b) / Math.min(a, b)Larger / smaller valueMath.max(10, 25) == 25
Math.pow(a, b) / Math.sqrt(x)Power & square rootMath.sqrt(81) == 9.0
Math.round(x), ceil, floorRounding operationsMath.ceil(4.2) == 5.0
Math.addExact(a, b)Throws ArithmeticException on overflowPrevents 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/urandom or Windows CryptGenRandom). Always use SecureRandom for OTPs, password reset tokens, API keys, and cryptographic salts.

Common Mistakes

  • Using Math.random() for security tokens or OTPs—Math.random() delegates to java.util.Random, which is not cryptographically secure.
  • Forgetting integer overflow in Math.abs(Integer.MIN_VALUE), which returns Integer.MIN_VALUE because +2,147,483,648 cannot fit in a 32-bit signed int.
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)