# Get random number between 0 and 1 in java

In this tutorial we will see how to get java random number between 0 and 1. To generate java random between 0 and 1 ,we will use java.lang.Math and java.util.Random class.

## Method 1: Java random Number between 0 and 1 Using Math.random

We will get java random between 0 and 1  using random method of java.lang.Math class.

Syntax :

Math.random()

Return Value : Math.random() method returns a positive double value , greater than or equal to 0.0 and less than 1.0.

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

System.out.println("Genearte java random between 0 and 1");

System.out.println("Random number 1 = " + Math.random());
System.out.println("Random number 2 = " + Math.random());
System.out.println("Random number 3 = " + Math.random());

System.out.println("Random number 4 = " + Math.random());
System.out.println("Random number 5 = " + Math.random());
System.out.println("Random number 6 = " + Math.random());
}
}

Output:

Genearte java random between 0 and 1
Random number 1 = 0.406363216017511
Random number 2 = 0.1806512753236963
Random number 3 = 0.6619925601074311
Random number 4 = 0.4939321963373827
Random number 5 = 0.14366851856020502
Random number 6 = 0.5539793156136271

## Method 2: Java random between 0 and 1 Using Random Class

We can create random number between 0 and 1 in java using  java.util.Random Class . There We will use nextFloat() and nextDouble() method of Random class to generate number between 0 and 1. We will create instance of java.util.Random and then call nextFloat() and nextDouble() method .

nextFloat() : It’s return a float between 0.0 and 1.0.
nextDouble() : It’s return a double between 0.0 and 1.0.

import java.util.Random;

public class RandomNumber0To1InJava2 {
public static void main(String[] args) {
Random random = new Random();

System.out.println("Generate random number using nextFloat()");
System.out.println("Random number 1 = "+random.nextFloat());
System.out.println("Random number 2 = "+random.nextFloat());
System.out.println("Random number 3 = "+random.nextFloat());

System.out.println("Generate random number using nextDouble()");
System.out.println("Random number 4 = "+random.nextDouble());
System.out.println("Random number 5 = "+random.nextDouble());
System.out.println("Random number 6 = "+random.nextDouble());

}
}

Output:

Generate random number using nextFloat()
Random number 1 = 0.057654977
Random number 2 = 0.71102303
Random number 3 = 0.43338472
Generate random number using nextDouble()
Random number 4 = 0.12222655652110881
Random number 5 = 0.7954556769965279
Random number 6 = 0.6696588574835786

In This tutorial we learned  How to get java random number between 0 and 1 . You can see more java program for practice .