# 2.9. 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 following code. 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.

Run the code below 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; Check your understanding Sample Problem

Other Math functions that you can use are:

• int abs(int) : Returns the absolute value of an int value (which is the value of a number without its sign, for example Math.abs(-4) = 4).

• 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!).

## 2.9.1. Programming Challenge : Random Numbers¶ You may have a combination lock on your locker at school where you have to spin the dial to 3 separate numbers from 0 up to 40. What if you forgot your combination? Would you be able to guess it?

1. Write code that will generate 3 random integers from 0 up to 40 (but not including 40) using Math.random() in the Active Code window below. Run it a couple times to see it generate different numbers.

2. How many times would you need to run it to guess your combination correctly? Let’s have the code compute the number of permutations possible in your combination lock using Math.pow(number,exponent). For example, if you had 2 dials on your combination lock where each dial can be set to a digit from 0-9 (10 digits), there are 102 possible permutations. In Java, this would be written as Math.pow(10,2) which means 10 to the power of 2. If you start listing all the permutations possible, you can tell that there are 102 or 100 possible permutations for a 2 dial lock from 0-9.

00, 01, 02, 03, 04, 05, 06, 07, 08, 09
10, 11, 12, 13, 14, 15, 16, 17, 18, 19
...
90, 91, 92, 93, 94, 95, 96, 97, 98, 99


Now what about the combination lock for this challenge? It has 3 dials with 0-40 (not including 40) numbers possible on each dial. In general, the formula to use is NumbersPerDialnumberOfDials. Write this using the Math.pow() method in your code and save it into a variable and print out.

Complete the combination lock challenge below.

Here’s another challenge that is a lot of fun! Can you use random numbers to make dancing turtles? This idea was suggested by Zac Martin’s class.

Complete the random numbers using Math.random() in the correct ranges to choose x, y coordinates for the turtle.

## 2.9.2. 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).