java tutorial - Java Math.random() Method - java programming - learn java - java basics - java for beginners



Java numbers random

Learn Java - Java tutorial - Java numbers random - Java examples - Java programs

Description

The method Math.random () - in Java is used to generate a random number in the range from 0.0 to 1.0. Different ranges can be achieved using arithmetic.

Syntax

static double random()
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • The default method does not take a parameter.

Return value

  • In Java Math.random () Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0 (0.0 <= Math.random () <1.0).

Example 1

public class Test { 

   public static void main(String args[]) {
      System.out.println( Math.random() );
      System.out.println( Math.random() );
   }
}
click below button to copy the code. By - java tutorial - team

Output

0.16763945061451657
0.400551253762343

Note:The result will change every time you call the Math.random () method.

Example2: a random number between 0 and 10

package com.javatutorialhq.java.examples;


/*
 * This example source code demonstrates the use of  
 * Random() method of Math class
 * Generating random numbers from 1 to 10
 */

public class MathRandomExample {

public static void main(String[] args) {

// define the range
int max = 10;
int min = 1;
int range = max - min + 1;

// generate random number from 1 to 10
int rand = (int) (Math.random()* range) + min;
System.out.println(rand);

}  java-random-advance

}
click below button to copy the code. By - java tutorial - team

Output

 formula deg to rad

Learn java - java tutorial - java random advance - java examples - java programs


Related Searches to Java Math.random() Method