Aral Code

Basics Lesson 8 of 21

Java Math Class and Random Numbers

Use Java's Math class — max, min, abs, sqrt, pow, round, ceil, floor — and generate random numbers with Math.random and the Random class.

Java's built-in Math class has ready-made methods for common calculations. You don't need to import it; just write Math. followed by the method name.

Common Math methods

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.max(88, 95));
        System.out.println(Math.min(88, 95));
        System.out.println(Math.abs(-15));
        System.out.println(Math.sqrt(144));
        System.out.println(Math.pow(2, 10));
        System.out.println(Math.round(89.5));
        System.out.println(Math.ceil(41 / 5.0));
        System.out.println(Math.floor(9.99));
        System.out.println(Math.PI);
    }
}

Output

95
88
15
12.0
1024.0
90
9.0
9.0
3.141592653589793
MethodDoesReturns
Math.max(a, b) / Math.min(a, b)Larger / smaller of twosame type
Math.abs(x)Removes the minus signsame type
Math.sqrt(x)Square rootdouble
Math.pow(a, b)a to the power bdouble
Math.round(x)Nearest whole number (.5 rounds up)long
Math.ceil(x) / Math.floor(x)Round up / downdouble

Math.ceil(41 / 5.0) answers "41 people, 5 per tricycle: how many tricycles?" Notice 5.0; with 41 / 5 the division would already be cut to 8 before ceil runs.

Rounding to 2 decimal places

public class Main {
    public static void main(String[] args) {
        double avg = 88.4567;
        double rounded = Math.round(avg * 100) / 100.0;
        System.out.println(rounded);
        System.out.printf("%.2f%n", avg);   // for display only
    }
}

Output

88.46
88.46

Multiply by 100, round, then divide by 100.0. If you only need to show the number, printf("%.2f") is simpler.

Example: right triangle and circle

public class Main {
    public static void main(String[] args) {
        double a = 6, b = 8;
        double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
        System.out.println("Hypotenuse: " + c);

        double r = 3.5;
        System.out.printf("Circle area: %.2f%n", Math.PI * r * r);
    }
}

Output

Hypotenuse: 10.0
Circle area: 38.48

Random numbers with Math.random()

Math.random() returns a random double from 0.0 up to (but not including) 1.0. To get a whole number from 1 to 6:

int dice = (int) (Math.random() * 6) + 1;

The general formula for a whole number from min to max:

int n = (int) (Math.random() * (max - min + 1)) + min;

The Random class

java.util.Random is easier to read and has more options:

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random rand = new Random();
        int dice = rand.nextInt(6) + 1;          // 1 to 6
        int classNo = rand.nextInt(1, 41);       // 1 to 40 (Java 17+)
        boolean coin = rand.nextBoolean();
        System.out.println("Dice: " + dice);
        System.out.println("Recitation: student #" + classNo);
        System.out.println("Coin: " + (coin ? "Heads" : "Tails"));
    }
}

Output

Dice: 4
Recitation: student #27
Coin: Tails

Your output will differ every run. nextInt(6) gives 0–5, so we add 1. nextInt(1, 41) gives 1 to 40; the upper number is excluded.

Example: a lotto quick pick

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random rand = new Random();
        System.out.print("Your 6/58 numbers: ");
        for (int i = 0; i < 6; i++) {
            System.out.print(rand.nextInt(1, 59) + " ");
        }
        System.out.println();
    }
}

This version may repeat a number. You'll fix that with arrays later in the course.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What type does Math.pow(2, 3) return?
2. What is Math.ceil(4.1)?
3. Which gives a random whole number from 1 to 10?
4. Do you need an import to use Math.sqrt?

By Sir Reyel · Last updated 2026-12-04