Working with Math and Random Numbers

1. Using Math Class Methods

MethodDescription
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.EConstants

2. Working with Trigonometric Functions

MethodInputOutput
sin/cos/tanRadiansdouble
asin/acos/atanRatioRadians
atan2(y, x)CoordsAngle to point
toRadians(deg) / toDegrees(rad)Conversion
sinh/cosh/tanhHyperbolic

3. Using Exponential Functions

MethodDescription
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

MethodResult for 2.5Result 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

MethodRange
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);
MethodReturns
nextInt()Any int
nextInt(bound)[0, bound)
nextLong/nextDouble/nextFloat/nextBooleanPer-type
nextGaussian()Normal distribution mean 0, sd 1
ints/longs/doublesStream of values

7. Using SecureRandom

AspectDetail
Classjava.security.SecureRandom
Use forTokens, passwords, crypto keys, salts
AlgorithmSecureRandom.getInstanceStrong()
APISame as Random + nextBytes(byte[])
Warning: Never use Math.random() or Random for security-sensitive values.

8. Understanding Random Number Seeds

BehaviorDetail
Same seedSame sequence (reproducible)
Default seedSystem time + counter
Set after creationrng.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);
MethodDescription
add/subtract/multiply/divide/modArithmetic (immutable)
pow(int)Exponentiation
modPow(e, m)Modular exponentiation
gcd(b)Greatest common divisor
compareTo(b)Order comparison
ConstantsZERO, 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
MethodDescription
new BigDecimal("0.1")String constructor (exact)
BigDecimal.valueOf(d)Use this for double — avoids float artifacts
add/subtract/multiplyExact arithmetic
divide(b, scale, mode)Specify rounding
setScale(s, RoundingMode)Decimal places
RoundingModeHALF_UP, HALF_EVEN, FLOOR, CEILING, UP, DOWN
Warning: Never construct via new BigDecimal(0.1) — uses inexact double. Use string or valueOf.