JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Java Math.random() Example

March 13, 2014 by Krishna Srinivasan Leave a Comment

In Java, there is a method random() in the Math class, which returns a double value between 0.0 and 1.0. Note that the default random numbers are always generated in between 0 and 1. If you want to get the specific range of values, the you have to multiple the retruned value with the magnitue of the range. For example, if you want to get the random numbers between 0 to 20, the the returned value has to be multiplied by 20 to get the desired result. Lets look at the simple example to understand this concept:

MathRandomExample.java

[code lang=”java”]
package javabeat.net.util;
public class MathRandomExample {
public static void main(String[] args) {
double x = Math.random();
double y = Math.random();
System.out.println("Random Number 1 : " + x);
System.out.println("Random Number 2 : " + x);
//Get the random number between 1-30
x = Math.random()*30;
System.out.println("Random Number between 0 – 30 : " + x);
//Convert to Integer
int xInt = (int)(Math.random()*30);
System.out.println("Random Number between 0 – 30 (Integer) : " + xInt);
//Random numbet between 5-15
xInt = 5 + (int)(Math.random()*10);
System.out.println("Random Number between 5 – 15 (Integer) : " + xInt);

}

}

[/code]

Output…

[code]
Random Number 1 : 0.7050742837772005
Random Number 2 : 0.7050742837772005
Random Number between 0 – 30 : 9.97783862360115
Random Number between 0 – 30 (Integer) : 20
Random Number between 5 – 15 (Integer) : 13
[/code]

If you look at the above code, Math.random() method generates the random number between 0-1. The next lines used that result and got the desired range of values like 0-30 and 5-15. I hope this example helps you to understand how to use the Math.random() utility method.

Filed Under: Java Tagged With: Core Java, Java Lang

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved