Generating Random Numbers with Math.random()


Generating Random Values Using The Math Class method random()

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;

This gives values between 0.0 and 5.99999999999999999999999

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);

With the cast, this gives values between 0 and 5.

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

This gives values between 1 and 6.


Generating Random Values Using 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 gen (short for generator) as our variable that references the Random object. We could have picked some other name.

Random gen = 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 = gen.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 = gen.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 = gen.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 = gen.nextInt(900) + 101;

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


What happens when we want to generate random integers in an unusual range where some values are negative and some values are positive, like -50 to 50?

All we have to do is look at the low end value of the range and add what is necessary to make -50 into 0. So we need to add 50 to -50 to give 0. Then we must add 50 to the upper end of the range ... 50 + 50 is 100. So now we have 0 to 100, so we put 101 in the ( ) of nextInt(), and then subtract 50 outside, since we added 50 to get the range to 0 to 100.

int x = gen.nextInt(101) - 50;


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 in that range, then we need to use the line of code:

double y = gen.nextDouble();

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

Notice as a trivial case, the line of code above could also be

double y = gen.nextDouble() * 1.0;

Since 0.0 is the lowest possible value that can be generated from nextDouble(), then   0.0 * 1.0 = 0.0   would be the lowerst possible

value generated in this case.

Since the highest possible value that can be generated from nextDouble() is up to but not including 1.0, like 0.9999999999 then the

highest possible value generated is 0.9999999999 * 1.0 = 0.9999999999

So the random value would be in the range from 0.0 to 0.9999999999

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 = gen.nextDouble() * 100.0;

Since 0.0 is the lowest possible value that can be generated from nextDouble(), then   0.0 * 100.0 = 0.0   would be the lowerst possible

value generated in this case.

Since the highest possible value that can be generated from nextDouble() is up to but not including 1.0, like 0.9999999999 then the

highest possible value generated is 0.9999999999 * 100.0 = 99.99999999

So the random value would be in the range from 0.0 to 99.99999999


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 = gen.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 = gen.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.


What happens when we want to generate a random floating-point value in an unusual range, like -21.5 to 33.8? As we did before when using nextInt() to generate values where some are negative and some are positive, all we have to do is look at the low end value of the range and add a value to make the range from 0.0 to something. Here if we added 21.5 to both ends of the range we have 0.0 to 55.3. So we know to multiply by 55.3 and then subtract the 21.5 that we initially added.

double y = gen.nextDouble() * 55.3 - 21.5;


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;