Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java random numbers

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

This tutorial focuses on:

The random() method

Generate random numbers using the random() method of the Math class. Random numbers returned will be of data type 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.

Generating a random number:
double randNumber = Math.random(); System.out.print(randNumber);

Why limit yourself to such a low range as between 0.0 and 1.0? You can actually return random numbers in any range. To do this multiply the returned value from a random() function by a number that when added to the lowest number in your range will produce the highest range. If this sounds confusing, not to worry, here is an example:

//will return a random number between 40.0(inclusive) and 90.0(exclusive) double randNumber = Math.random() * 50 + 40; /* The random() method naturally returns a random number between 0.0 and 1.0. That value is then multiplied by 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. */

The random() method returns decimal numbers, but you can also return whole numbers. This is accomplished by data type casting the value returned by the Math.random() method to data type int.

Example:
//return a random number between 10.0(inclusive) and 60.0(exclusive) //and cast it to a whole number int aNumber = (int) (Math.random() * 50 + 10);

Placing the word int like that in parentheses before the random() function and also placing the random() function in parentheses will convert the value returned by the random() function from a double to a whole number. This process is known as casting.

Using the Random class to generate random numbers

The Random class is located in the java.util package. It can be used generate random numbers of different data types (double, float, int, long) as well as random booleans.

Methods of the Random class:

NOTE: When you use Math.random() to generate a random double, it actually creates a Random object and calls the nextDouble() method.

A bunch of randomness using the Random class:
import java.util.Random; class GenerateRandomValues{ public static void main(String[] args){ Random aRandom = new Random(); System.out.println(aRandom.nextInt()); //print a random number between 1 and 10 System.out.println(aRandom.nextInt(10) + 1); System.out.println(aRandom.nextDouble()); System.out.println(aRandom.nextBoolean()); } }
Output:
235659484 9 0.8259070568528002 true

The psuedorandom process

It seems that these random numbers (and other random values) are really random, but they are not. The Random class actually generates pseudorandom 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 another value based on the seed.

By default, the seed given to a Random object is the value of the system clock in milliseconds (which is your computer's interpretation of the current time). Since this is constantly changing, the seed is always changing and therefore a different "random" number is always being generated.

If you set the seed yourself to a fixed number, then the "random" numbers generated will always be the same like in this example below.

Setting the seed to a fixed number:
import java.util.Random; class GenerateRandomNumbers{ public static void main(String[] args){ Random aRandom = new Random(); //set the seed of aRandom to 1015 aRandom.setSeed(1015); //the number generated will not change //since the seed is always the same System.out.println(aRandom.nextInt()); } }
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information