Java
Java intro
Java basics
Java variables
Java conditionals
Java loops
Java arrays
Java OOP 1
Java OOP 2
Java interfaces
Java Strings
Java user input
Java exceptions
Java packages
Java rand numbers
Java GUI's
Java GUI layout
Java events
Java applets
Java graphics
Java sounds
Java summary

Programming

Programming intro

Markup

First webpage guide HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Java random numbers

Random numbers from very low to very high ranges can be generated with Java.

This lesson focuses on:

Using the Math class to generate random numbers

You can generate random numbers using the Math class by using its random() method. The random() method is a method that returns a random double from 0.0(inclusive) to 1.0(exclusive) - meaning that the random number generated will be greater than or equal to 0.0 and less than 1.0.

Example:

class GetRandomNumber{

    public static void main(String[] args){

        double randNumber = Math.random();
        System.out.println(randNumber);
    
    }

}

In the above example, the variable randNumber is declared as type double and its value is set to a random number it gets from the random() method. The line of code after that will print a random number between 0.0(inclusive) and 1.0(exclusive).

You can use the random() method to return random numbers of even higher ranges.

Syntax:

double doubleVariable = Math.random() * 
number_Added_To_Lower_Range_To_Produce_Higher_Range + 
lowerRange;

Example:

class GetRandomNumber{

    public static void main(String[] args){

        //set randNumber to be a 
        //random number between 40 and 90
        double randNumber = Math.random() * 50 + 40;

        //print the value of randNumber
        System.out.println(randNumber);
    
    }

}

The above example will return a random number between 40.0(inclusive) and 90.0(exclusive). The random() method naturally returns a random number between 0.0 and 1.0. That value is then multiplied by 50 since 90 - 40 = 50. The difference between the lower and upper limit now ranges from 0.0. to 50.0. When 40 is added to that value, the range becomes 40.0 to 90.0.

You can also use the random() method to return random numbers that are not doubles, but whole numbers. This is accomplished by data type casting the value returned by the Math.random() method to data type int.

Example:

class GetRandomNumber{

    public static void main(String[] args){

        //return a random number between 10 and 60
        //and cast it to a whole number
        //set the variable aNumber to this number
        int aNumber = (int) (Math.random() * 50 + 10);

        //print the value of aNumber
        System.out.println(aNumber);

    }

}

In the above example, a variable named aNumber of data type int is declared. The random() method is set to return a random number from 10.0 to 60.0. The (int) placed before it will convert that value to a whole number and assign the value to the aNumber variable. aNumber will be a whole number between 10 and 60.

Using the Random class to generate random numbers

The Random class is located in the java.util package. To use the Random class, you will need to import this package. The Random class can be used generate random numbers of different data types (double, float, int, long) as well as random booleans.

Declaring an instance of the Random class:

Example:

Random aRand = new Random();

Methods of the Random class:

Example:

import java.util.Random;

class GenerateRandomNumbers{

    public static void main(String[] args){

    //declare a new instance of the Random class
    Random aRandom = new Random();

    //print a random number
    System.out.println(aRandom.nextInt());

    //print a random number between 1 and 10
    System.out.println(aRandom.nextInt(10) + 1);

    //print a random double
    System.out.println(aRandom.nextDouble());

    //print a random float
    System.out.println(aRandom.nextFloat());

    //print a random long
    System.out.println(aRandom.nextLong());
    
    //print a random boolean value
    System.out.println(aRandom.nextBoolean());

    }

}

The psuedorandom process

While it may seem that random numbers generated by the Random class really are random, they are not. The Random class actually generates psuedorandom numbers - numbers generated in a non random way, but in a way that makes them look random. This is accomplished by the Random class taking an initial value (called a seed) and through a specific algorithm generating more values based on the seed. By default, the seed given to a Random object is the value of the system clock in milliseconds.

When you use the random() method of the Math class it also generates psuedorandom numbers! This is because the random() method of the Math class automatically creates a Random object and calls its nextDouble() method when you use it. This same Random object will be used when you use the random() method of the Math class throughout a program.

Setting a seed

You can set a seed for a Random object by using the setSeed() method.

Example:

import java.util.Random;

class GenerateRandomNumbers{

    public static void main(String[] args){

    //declare a new instance of the Random class
    Random aRandom = new Random();

    //set the seed of aRandom to 1015
    aRandom.setSeed(1015);

    //print a random number
    System.out.println(aRandom.nextInt());

    }
}

In the above example, the seed for the aRandom object will be 1015. Based on this value, a set of finite non-random ordered set of values will be generated.

NOTE: The seed specified in the above example (1015) will not be the first number generated! Instead, the Random class will take this value and through a specific algorithm will generate more values based on it.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments