---
title: Java Math random() Method- Scaler Topics
description: In this article by Scaler Topics, you will learn Math.random() method in Java helps us to generate random double values between 0 and 1.
author: Agam Jyot Singh
category: Java
---
:::section{.abstract}
We frequently need to create random numbers while developing applications in Java programming. Several applications (for instance, a **dice roller** app) often need to create random numbers. This is where we use `Math.random()`.
The `Math.random()` in java helps us to generate random double values between `0` and `1`.
:::
:::section{.main}
## Syntax
**Method Signature:**
```java
public static double random()
```
**Syntax:**
```java
Math.random()
```
Here, Math is the class and `random()` is the `method`.
:::
:::section{.main}
## Return Type
The function returns only double type random numbers between `0.0` and `1.0`, which of course, can be type cast to `int`, or `long`.
:::
:::section{.main}
## Java Math random() Method Examples
To understand `Math.random` in java better, let's look at some examples :
### Example : 1
The below example shows how to generate `three` random numbers using `Math.random()` method in java. You may use the same to generate more numbers if you want!
```java
import java.util.Math;
class Scaler {
public static void main(String[] args) {
// Printing the first random value
System.out.println(Math.random());
// Printing the second random value
System.out.println(Math.random());
// Printing the third random value
System.out.println(Math.random());
}
}
```
**Output :**
```plaintext
0.24764727798989705
0.8010378285494902
0.2729991552127389
```
### Example : 2
In this example, we will try to generate five random numbers between `15` and `30` :
```java
import java.util.Math;
class Scaler {
public static void main(String[] args) {
int min = 15;
int max = 30;
// Please note that the upperBound (in this case 30) will also be included
int range = (max - min) + 1;
System.out.println("Five random values between 15 and 30:");
for (int i = 0; i < 5; i++) {
// Here, we use "(int)" to convert double values into int
int random = (int)(Math.random() * range) + min;
System.out.print(random + " ");
}
}
}
```
**Output:**
```plaintext
Five random values between 15 and 30:
26 27 25 22 19
```
### Example : 3
In this example, we will initialize an array and use `Math.random()` to access any `5` elements from the array :
```java
import java.util.Math;
class Scaler {
public static void main(String[] args) {
// First, let's create an array
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// n = Number of elements in the array
int n = arr.length;
System.out.println("Five random elements from the array:");
// Now, we can access any 5 random array elements from the array that we initialised
for (int i = 0; i < 5; i ++) {
// To do that, we will obtain random array index values
int random = (int)(Math.random() * n);
System.out.print(arr[random] + " ");
}
}
}
```
As clearly visible in the output below, we obtain five random numbers from the array provided :
**Output:**
```plaintext
Five random elements from the array:
3 4 1 5 2
```
:::
:::section{.summary}
## Conclusion
1. The `Math.random` method in Java provides random numerical values when it is called.
2. This, however, will produce double values in the range of `0.0` to `1.0`, and not integers.
3. However, to obtain integers, you may use implicit or explicit type-casting to obtain values according to your requirements.
:::