Katerina Zamani

About Katerina Zamani

Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.

Java math.random Example

Java provides us Math class, which includes methods with basic numeric operations such as logarithm, square root etc. One of these methods is random(), which give us a pseudorandom positive double number greater than or equal to 0.0 and less than 1.0 – [0.0, 1.0).

In this example we are going to show how to produce integer and double random numbers between a defined space, via random() method.

 
 
 

1. Example of Math.random method

Create a java class with name MathRandomClass and paste the following code.

MathRandomClass.java:

package com.javacodegeeks.javacore.mathrandom;

public class MathRandomClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// a simple random number
		double x = Math.random();
		System.out.println("Double between 0.0 and 1.0: x = "+x);
		
		// double between [0.0, 20.0)
		double y = Math.random()*20.0;
		System.out.println("Double between 0.0 and 20.0: y = "+y);
		
		// integer between [3,7]
		int r1 = (int) (Math.random()*5)+3;
		System.out.println("Integer between 3 and 8: r1 = "+r1);
		
		// integer between [-10,10) - maximum 9
		int r2 = (int) (Math.random()*20)-10;
		System.out.println("Integer between -10 and 10: r2 = "+r2);
		
	}

}

Now lets explain the code above. Firstly, we use the random() method in order to take a positive signed double value, that belongs into the range [0.0, 0.1). For generating random integer or double numbers between a range, we should multiply and/or sum the appropriate positive and/or negative values in order to achieve the desirable result. For example, if we want a random positive, double number greater than or equal to 0.0 but less than 20.0, we should multiply the result of random() method with the double number 20.0. Respectively, if we multiply the result with 5 and add number 3, the range has as minimum value number 3 and as maximum value their sum (5+3). Please notice that the random values are always less than this sum – in our example the range is [3,8). Also notice that for integer values we should cast the result. We can use the same way for negative random numbers etc.

For a better understanding, please look the output of the execution. As you can notice, all the parameters take a value that is into their respective range.

Output:

Double between 0.0 and 1.0: x = 0.9749288905833321
Double between 0.0 and 20.0: y = 14.067387697238761
Integer between 3 and 8: r1 = 7
Integer between -10 and 10: r2 = -9

Download the source file

This was a tutorial about Math.random() in Java. Download the source code of this example: MathRandomExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close