Generating Random Numbers


The Random Class.

In Java, there is more than one way to generate random numbers. We will concentrate on one method that you are required to know for the AP Exam. It uses a class called Random from the java.util package. Therefore you must use the following import statement at the first of a program:

import java.util.Random;

Before you try to generate a random whole number (int) or floating-point (double) value, you must declare a variable that can reference a Random object then you must instantiate a new Random object that can do the generating. Here is how to do it. We decide to use the name generator as our variable that references the Random object. We could have picked some other name.

Random generator = new Random();

Next, we need to decide whether we want to generate a random integer value or a random floating-point value.


Generating Random Integer Values.

If we want to generate a random integer value, we need to use the nextInt method and pass it one parameter and we need to tell Java what range we want the random integer to be within. If we wish to generate a random integer between 0 and 9 inclusive, then we need to use the line of code:

int x = generator.nextInt(10);

Notice that 10 is not included as one of the possible random integer values, but beginning with 0 all the other integers up to 10 are. You could think of it this way ... generate me a random integer within the next 10 integers beginning with and including 0. So, if I want to generate random integer values between 0 and 153, I would use:

int x = generator.nextInt(154);


What happens when we want to generate random integers in an unusual range, like 101 - 1000? All we have to do is look at the low end value of the range and subtract it from both the low end and high end range values. Here if we subtracted 101 from both ends of the range we have 0 - 899. We know how to generate a random integer for this range by using:

int x = generator.nextInt(900);

We then take the 101 we initially subtracted and add it onto the end of the statement that generates the random integers to get:

int x = generator.nextInt(900) + 101;

After the random value is generated, by adding the 101, we get values within the desired range of 101 - 1000.


Generating Random Floating-Point Values.

If we want to generate a random floating-point value, we need to use the nextDouble method. When we use it we don't pass the method any values, because a call to nextDouble() always returns a value between 0.0 inclusive to 1.0 exclusive, where 0.0 is included in the range but 1.0 is not.

So if we wish to generate a random floating-point value between 0.0 and 1.0, then we need to use the line of code:

double y = generator.nextDouble();

As stated before, notice that no value is passed this method!

If we wish to generate a random floating-point value between 0.0 inclusive to 100.0 exclusive, then we need to use the line of code:

double y = generator.nextDouble() * 100;

Since 0.0 is the lowest possible value that can be generated from nextDouble(), 100 * 0.0 = 0.0

Since 1.0 is the highest possible value that can be generated from nextDouble(), 100 * 1.0 = 100.0


What happens when we want to generate a random floating-point value in an unusual range, like 10.3- 40.8? As we did before when using nextInt(), all we have to do is look at the low end value of the range and subtract it from both the low end and high end range values. Here if we subtracted 10.3 from both ends of the range we have 0 - 30.5. We know how to generate a random floating-point value for this range by using:

double y = generator.nextDouble() * 30.5;

We then take the 10.3 we initially subtracted and add it onto the end of the statement that generates the random integers to get:

double y = generator.nextDouble() * 30.5 + 10.3;

After the random value is generated, by adding the 10.3, we get values within the desired range of 10.3 inclusive - 40.8 exclusive.


Additional Examples.

 

Example 1. Write the lines of code necessary to generate a random whole number between 1 and 10 inclusive. You must declare all variables needed and instantiate any objects needed. You must store the value after generated in an appropriate variable.

Random gen1 = new Random();

int x = gen1.nextInt(10) + 1;

 

Example 2. Write the lines of code necessary to generate a random whole number between 10 and 30 inclusive. You must declare all variables needed and instantiate any objects needed. You must store the value after generated in an appropriate variable.

Random gen2 = new Random();

int y = gen2.nextInt(21) + 10;

 

Example 3. Write the lines of code necessary to generate a random floating point number between 0.0 inclusive and 10.0 exclusive. You must declare all variables needed and instantiate any objects needed. You must store the value after generated in an appropriate variable.

Random rating1 = new Random();

double z = rating1.nextDouble() * 10;

 

Example 4. Write the lines of code necessary to generate a random floating point number between 1.0 inclusive and 3.0 exclusive. You must declare all variables needed and instantiate any objects needed. You must store the value after generated in an appropriate variable.

Random rating2 = new Random();

double r = rating2.nextDouble() * 2 + 1;


Generating Random Values Using The Math Class.

There is another way to generate random values besides using nextInt() and nextDouble().   It involves using the random() method of the Math class.

a) Here is an example of how to generate a random double value:

double d = Math.random();

The random() method of the Math class generates a random double value in the range from 0.0 inclusive to 1.0 exclusive.  

In other words, 0 <= d < 1.0

To generate random double values in the range   0 <= d < 6.0   we would use:

double d = Math.random() * 6;

b) To generate random integers between 0 and 5 inclusive, we need to use casting since there are not separate methods to generate random integers and random doubles.   Here is an example:

int i = (int) (Math.random() * 6);

If we actually need random values between 1 and 6 inclusive, then we add 1 after the cast is complete:

int i = (int) (Math.random() * 6)   + 1;                         // note casting has a higher precedence than addition