Random Numbers

Mathematics in Java

Introduction

Imagine you have a series of numbers, such these: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, and 20. Imagine you want to select one of these numbers, any of them. A number is referred to as random if it has been selected from a pool without a specific pattern to follow. For example, if you decide to select the value 17 from this list, if there was an exact reason that number was selected, then it is not considered random. In reality, it is difficult for a number to qualify as random. For this reason, most random numbers are referred to as pseudo-random.

Getting a Random Number

To support the ability to create or choose a random number, the java.util package contains a class named Random. To start, you can declare a variable of this class, using one of its two constructors. Here is an example that uses the default constructor:

import java.util.Random;

public class Exercise {
    public static void main(String[] args) {
	Random rndNumber = new Random();
    }
}

After declaring the variable, you can start getting numbers from it. The Random class allows you to get a Boolean value, a normal integer, a long integer, or a decimal value. To assist with this, the Random class provides the following methods:

Method
Produces

boolean nextBoolean();

A true or false value

int nextInt()

An integral value between Integer.MIN_VALUE andInteger.MAX_VALUE

long nextLong()

A long integral value between Long.MIN_VALUE andLong.MAX_VALUE

float nextFloat()

A decimal number between 0.0 (included) and 1.0 (excluded)

double nextDouble()

A decimal number between 0.0 (included) and 1.0 (excluded)

Here is an example of calling one of these methods:

import java.util.Random;

public class Exercise {
    public static void main(String[] args) {
	Random rndNumbers = new Random();
        int    rndNumber  = rndNumbers.nextInt();

        System.out.println("Number: " + rndNumber);
    }
}

Here is an example of running the program:

Number: 1369872590

In the same way, you can call the method repeatedly to get random numbers. Here is an example:

import java.util.Random;

public class Exercise {
    public static void main(String[] args) {
	Random rndNumbers = new Random();
        int rndNumber = 0;

        for (int nbr = 1; nbr < 9; nbr++) {
            rndNumber = rndNumbers.nextInt();
            System.out.println("Number: " + rndNumber);
        }
    }
}

Here is an example of running the program:

Number: 1783601039
Number: -646235293
Number: -57529965
Number: 983241665
Number: 1794072434
Number: 1797572448
Number: 1057090973
Number: -634503464

The Seed of a Random Number

Consider the following program:

import java.util.Random;

public class Exercise {
    public static void main(String[] args) {
	Random rndNumbers = new Random();
        int rndNumber = rndNumbers.nextInt();

        System.out.println("Number: " + rndNumber);
    }
}

Here is an example of running the program:

Number: 573991745

Here is another example of running the same program:

Number: 334223329

Notice that the numbers generated are different. When creating a program that repeatedly gets a series of random numbers, you may (or may not) want theRandom class to generate the same number over and over again. A seed is a constant value that controls whether a random generation would produce the same result every time it occurs. For example, using a seed, you can impose it upon theRandom class to generate the same number every time the nextInt() method is called. To support the ability to use a seed, the Random class is equipped with a second constructor whose syntax is:

public Random(long Seed);

Based on this, to specify a seed, when declaring a Random variable, pass a constant integer to the constructor. Here is an example:

import java.util.Random;

public class Exercise {
    public static void main(String[] args) {
	Random rndNumbers = new Random(20);
        int rndNumber = rndNumbers.nextInt();

        System.out.println("Number: " + rndNumber);
    }
}

Here is one example of running the program:

Number: 375271809

Here is another example of running the same program:

Number: 375271809

Notice that the numbers are the same. Consider this program also:

import java.util.Random;

public class Exercise {
    public static void main(String[] args) {
	Random rndNumbers = new Random(20);
        int rndNumber = 0;

        for (int nbr = 1; nbr < 5; nbr++) {
            rndNumber = rndNumbers.nextInt();
            System.out.println("Number: " + rndNumber);
        }
    }
}

Here is one example of running the program:

Number: 375271809
Number: 1472524622
Number: 1605850688
Number: 1776011503

Here is another example of running the same program:

Number: 375271809
Number: 1472524622
Number: 1605850688
Number: 1776011503

Notice that the sequences are the same. In both cases, this indicates that, if you specify a seed, the Random class would generate the same number or the same sequence of numbers.

Besides the second constructor, to specify the seed, you can call thesetSeed() method. Its syntax is:

void setSeed(long seed);

This method takes one argument as the seed. Here is an example of calling it:

import java.util.Random;

public class Exercise {
    public static void main(String[] args) {
	Random rndNumbers = new Random();

	rndNumbers.setSeed(20);
        int rndNumber = rndNumbers.nextInt();

        System.out.println("Number: " + rndNumber);
    }
}

Generating Random Numbers in a Range of Numbers

So far, we have been using any number that would fit an integer. In some assignments, you may want to restrict the range of numbers that can be extracted. The Random class can assist with this. Using the Random class, you can generate random positive numbers from 0 (included) to a maximum (excluded) of your choice. To support this, the Random class is equipped with another version of the nextInt() method whose syntax is:

public int nextInt(int maxValue);

The argument to pass to the method determines the highest integer that can be generated by the nextInt() method. The method returns an integer. Here is an example that generates random numbers from 0 to 20:

import java.util.Random;

public class Exercise {
    public static void main(String[] args) {
	Random rndNumbers = new Random();
        int rndNumber = 0;


        for (int nbr = 1; nbr < 9; nbr++) {
            rndNumber = rndNumbers.nextInt(20);
            System.out.println("Number: " + rndNumber);
        }
    }
}

Here is an example of running the program:

Number: 1
Number: 7
Number: 1
Number: 16
Number: 14
Number: 19
Number: 3
Number: 1

The above version of the Next() method generates numbers starting at 0.

The Math.Random Method

Besides the Random class, the Math class is equipped with a method that can be used to generate random numbers. The syntax of this method is:

static double random();

When called, this method produces a randomly selected decimal number between 0.00 (included) and 1.00 (excluded).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: