# Java Summary: Math and java.util.Random Classes

Some basic math functions can be found in the Math class.
The `StrictMath`

class (not described here) may produce less accurate results and be slower,
but will produce absolutely identical results, bit for bit, on
all machines. Don't use `StrictMath`

unless you need this very specific
kind of portability.

If you need numbers that exceed the range of `long`

, use `java.math.BigInteger`

.
If you need to do exact decimal arithmetic, use `java.math.BigDecimal`

,
which gives complete control over precision and rounding, eg for financial calculations.
Assume following declarations

float f; double d, d1, d2; double ar; // angle in radians.xis any of int, long, float, or double.

Math Constants | ||

Two common constants are defined in the Math class. | ||

double | Math.E() |
Value of e, 2.718282..., base of the natural logarithms. |

double | Math.PI() |
Value of pi, 3.14159265 .... |

Math Methods | ||

Trigonometric Methods | ||

All trigonometric method parameters are measured
in radians, the normal mathematical system of angles, and not in degrees,
the normal human angular measurement system. Use the toRadians or toDegrees
methods to convert between these systems, or use the knowledge that there
are 2*PI radians in 360 degrees. In addition to those below, the arc
functions are also available. | ||

double | Math.sin(ar) |
Returns the sine of ar. |

double | Math.cos(ar) |
Returns the cosine of ar. |

double | Math.tan(ar) |
Returns the tangent of ar. |

double | Math.toRadians(d) |
Returns d (angle in degrees) converted to radians. |

double | Math.toDegrees(ar) |
Returns ar (angle in radians) converted to degrees. |

Exponential Methods | ||

The two basic functions for logarithms and power are available.
These both use the base e (Math.E) as is the usual case in mathematics.
| ||

double | Math.exp(d) |
Returns e (2.71...) to the power d. |

double | Math.pow(d1, d2) |
Returns d1^{d2}. |

double | Math.log(d) |
Returns the logarithm of d to base e. |

Misc Methods | ||

double | Math.sqrt(d) |
Returns the square root of d. |

t | Math.abs(x) |
Returns absolute value of x with same type as
the parameter: int, long, float, or double. |

t | Math.max(x, y) |
Returns maximum of x and y with same type as
the parameter: int, long, float, or double. |

t | Math.min(x, y) |
Returns minimum of x and y with same type as
the parameter: int, long, float, or double. |

Integer Related Methods | ||

The following methods translate
floating point values to integer values, altho these values
may still be stored in a double. | ||

double | Math.floor(d) |
Returns the closest integer-valued double which is
equal to or less than d. |

double | Math.ceil(d) |
Returns the closest integer-valued double which is
equal to or greater than d. |

double | Math.rint(d) |
Returns the closest integer-valued double to d. |

long | Math.round(d) |
Returns the long which is closest in value to the double d. |

int | Math.round(f) |
Returns the int which is closest in value to the float f. |

Random Numbers | ||

For significantly more control over random number generation
use the java.util.Random class (see below). | ||

double | Math.random() |
Returns a number x in the range, 0.0 <= x < 1.0. |

java.util.Random Class | ||

The java.util.Random class provides more flexible
ways to generate uniformly distributed random numbers, providing easy generation of types
other than double, as well as providing a Gaussian distribution. | ||

Random Constructors | ||

It's necessary to construct a Random object. | ||

Random | new Random(); |
Uses time in milliseconds as the seed. |

Random | new Random(long seed); |
Uses the provided seed for testing purposes. |

Random Methods | ||

All methods return a uniform distribution of values, except
.`nextGaussian()` . Assume r is a Random object. | ||

int | r.nextInt(int n) |
Returns random int >= 0 and < n. |

int | r.nextInt() |
Returns random int (full range). |

long | r.nextLong() |
Returns random long (full range). |

float | r.nextFloat() |
Returns random float >=0.0 and < 1.0. |

double | r.nextDouble() |
Returns random double >=0.0 and < 1.0. |

boolean | r.nextBoolean() |
Returns random boolean (true or false). |

double | r.nextGaussian() |
Returns random double with mean 0.0 and standard deviation 1.0. |

## Other math libraries

I don't keep track of math libraries, but I'll add links below as I run across them. I have no idea if these are good or bad.

- jakarta.apache.org/commons/math/ - Apache Jakarta Math library.
- www.ia.hiof.no/~kyvh/SciLib/docs/scilib.html - SciLib

Copyright 2005 Fred Swartz