Using the Math Class

Games would be boring if the same thing happened each time you played the game. Games often use random numbers to generate different possibilities. You need to know how to use the Math.random() method to generate a random number.

There are lots of mathematical methods that you might want to use in your programs like Math.abs (absolute value). These methods are in the Math class defined in the java.lang package. These are static methods which means you can call them by just using ClassName.methodName() without creating an object or just the method name if they are called from within the same class.

Note

Static methods (also called class methods) are called using the class name and the dot operator . followed by the method name, for example Math.random(). You do not need to create an object of the class to use them.

The Math.random() method returns a number greater than or equal to 0.0, and less than 1.0.

Try out the E01Random program. Run it several times to see what it prints each time.

You can use Math.random and a cast to integer to return a random integer between some starting and ending value. The code below will create a random integer from 0 to 9. Remember that casting a double value to integer (int) will throw away any values after the decimal point.

coding exercise Coding Exercise

Run the E02RandomRange program several times to see how the value changes each time. The program returns a random integer between 0 and 9, inclusive. How could you change the code to return a random integer from 1 to 10, inclusive? Modify the code and see if your answer is correct.

Note

  • Math.random() returns a random number between 0.0-0.99.

  • (int)(Math.random()*range) + min moves the random number into a range starting from a minimum number.

  • The range is the (max number - min number + 1).

Here are some examples that move a random number into a specific range.

// Math.random() returns a random number between 0.0-0.99.
double rnd = Math.random();

// rnd1 is an integer in the range 0-9 (including 9).
int rnd1 = (int)(Math.random()*10);

// rnd2 is in the range 1-10 (including 10). The parentheses are necessary!
int rnd2 = (int)(Math.random()*10) + 1;

// rnd3 is in the range 5-10 (including 10). The range is 10-5+1 = 6.
int rnd3 = (int)(Math.random()*6) + 5;

// rnd4 is in the range -10 up to 9 (including 9). The range is doubled (9 - -10 + 1 = 20) and the minimum is -10.
int rnd4 = (int)(Math.random()*20) - 10;

exercise Check your understanding

exercise Sample Problem

Other Math functions that you can use are:

Summary

  • Static Math methods can be called using Math.method(); for each method.

  • The following static Math methods are part of the Java Quick Reference:

    • int abs(int) : Returns the absolute value of an int value (which means no negatives).

    • double abs(double) : Returns the absolute value of a double value.

    • double pow(double, double) : Returns the value of the first parameter raised to the power of the second parameter.

    • double sqrt(double) : Returns the positive square root of a double value.

    • double random() : Returns a double value greater than or equal to 0.0 and less than 1.0 (not including 1.0)!

  • The values returned from Math.random can be manipulated to produce a random int or double in a defined range.

  • (int)(Math.random()*range) + min moves the random number into a range starting from a minimum number. The range is the (max number - min number + 1). For example, to get a number in the range of 5 to 10, use the range 10-5+1 = 6 and the min number 5: (int)(Math.random()*6) + 5).

You have attempted of activities on this page