Working with Math and Random Numbers
1. Using Math Class Methods
| Method | Description |
|---|---|
Math.abs(x) | Absolute value |
Math.max(a,b) / min(a,b) | Comparison |
Math.pow(b, e) | Power |
Math.sqrt(x) / cbrt(x) | Roots |
Math.floorDiv(a,b) / floorMod(a,b) | Floor division |
Math.signum(x) | -1.0, 0.0, 1.0 |
Math.addExact(a,b) | Throws on overflow |
Math.PI / Math.E | Constants |
2. Working with Trigonometric Functions
| Method | Input | Output |
|---|---|---|
sin/cos/tan | Radians | double |
asin/acos/atan | Ratio | Radians |
atan2(y, x) | Coords | Angle to point |
toRadians(deg) / toDegrees(rad) | Conversion | — |
sinh/cosh/tanh | Hyperbolic | — |
3. Using Exponential Functions
| Method | Description |
|---|---|
Math.exp(x) | e^x |
Math.log(x) | Natural log |
Math.log10(x) | Base-10 log |
Math.log1p(x) | ln(1+x), accurate near 0 |
Math.expm1(x) | e^x - 1, accurate near 0 |
4. Rounding Numbers
| Method | Result for 2.5 | Result for -2.5 |
|---|---|---|
Math.round(x) | 3 | -2 |
Math.floor(x) | 2.0 | -3.0 |
Math.ceil(x) | 3.0 | -2.0 |
Math.rint(x) | 2.0 (banker's) | -2.0 |
5. Generating Random Numbers
| Method | Range |
|---|---|
Math.random() | [0.0, 1.0) |
(int)(Math.random() * n) | [0, n) |
ThreadLocalRandom.current().nextInt(min, max) | [min, max) |
RandomGenerator.getDefault() JAVA 17+ | Modern factory API |
6. Using Random Class
Example: Random
Random rng = new Random(); // unseeded
Random seeded = new Random(42L);
int dice = rng.nextInt(6) + 1; // 1..6
double d = rng.nextDouble(); // [0,1)
boolean b = rng.nextBoolean();
rng.ints(10, 0, 100).forEach(System.out::println);
| Method | Returns |
|---|---|
nextInt() | Any int |
nextInt(bound) | [0, bound) |
nextLong/nextDouble/nextFloat/nextBoolean | Per-type |
nextGaussian() | Normal distribution mean 0, sd 1 |
ints/longs/doubles | Stream of values |
7. Using SecureRandom
| Aspect | Detail |
|---|---|
| Class | java.security.SecureRandom |
| Use for | Tokens, passwords, crypto keys, salts |
| Algorithm | SecureRandom.getInstanceStrong() |
| API | Same as Random + nextBytes(byte[]) |
Warning: Never use
Math.random() or Random for security-sensitive values.8. Understanding Random Number Seeds
| Behavior | Detail |
|---|---|
| Same seed | Same sequence (reproducible) |
| Default seed | System time + counter |
| Set after creation | rng.setSeed(s) |
9. Working with BigInteger
Example: BigInteger
BigInteger a = new BigInteger("123456789012345678901234567890");
BigInteger b = BigInteger.TEN.pow(50);
BigInteger sum = a.add(b);
BigInteger gcd = a.gcd(b);
boolean prime = a.isProbablePrime(20);
| Method | Description |
|---|---|
add/subtract/multiply/divide/mod | Arithmetic (immutable) |
pow(int) | Exponentiation |
modPow(e, m) | Modular exponentiation |
gcd(b) | Greatest common divisor |
compareTo(b) | Order comparison |
| Constants | ZERO, ONE, TWO, TEN |
10. Working with BigDecimal
Example: Money math
BigDecimal price = new BigDecimal("19.99");
BigDecimal qty = BigDecimal.valueOf(3);
BigDecimal total = price.multiply(qty)
.setScale(2, RoundingMode.HALF_UP); // 59.97
| Method | Description |
|---|---|
new BigDecimal("0.1") | String constructor (exact) |
BigDecimal.valueOf(d) | Use this for double — avoids float artifacts |
add/subtract/multiply | Exact arithmetic |
divide(b, scale, mode) | Specify rounding |
setScale(s, RoundingMode) | Decimal places |
| RoundingMode | HALF_UP, HALF_EVEN, FLOOR, CEILING, UP, DOWN |
Warning: Never construct via
new BigDecimal(0.1) — uses inexact double. Use string or valueOf.