# Java Random Number Generator

The Java random generator is one of the Math Library functions used to generate and return the Pseudo-random number between Zero and One. The basic syntax of the Math random is as shown below.

```static double random(); //Return Type is Double

// In order to use in program:
Math.random();```

## Java Random Number Generator Example

The Math.random Function returns the Pseudo-random numbers between 0 to 1. In this program, We are going to use that function and display the output.

```package MathFunctions;

public class RandomMethod {
public static void main(String[] args) {

System.out.println(Math.random());
System.out.println(Math.random());

System.out.println(Math.random());
System.out.println(Math.random());
}
}```

The value generated by the random function is from 0 (included) and less than 1. If you observe the below output, We called the Function four times, and it is returning four different values.

``````0.03922519673249125

0.6550200808825295

0.9178026557745128

0.8929566570868191``````

### Example 2

In this program, we will show how to store random values into an array. Here, we are going to declare an array of double types and fill that array with distinct values generated by Math.random.

First, We declared an empty double Array. Next, We used Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Here, the compiler will check for the condition (i < 10).

The following Java Random Number Generator function statements will store the values in an array. If you observe the code snippet, we are assigning each index position with one value.

Here, the compiler will call the Math function (static double random() ) to return a value between 0 and 1.

Next, to display the array values, We used another For Loop to iterate the Array. Within the For Loop, we initialized i value as 0, and the compiler will check the condition (i < myArray.length). The myArray.length finds the length of an array.

Within the loop, the Java System.out.println statements prints the output.

```package MathFunctions;

public class RandomMethodInArray {
public static void main(String[] args) {

double [] myArray = new double;

for (int i = 0; i < 10; i++) {
myArray[i] = Math.random();
}
//Displaying Result
for (int i = 0; i < myArray.length; i++) {
System.out.println("Array Element = " + myArray[i]);
}
}
}```