The random() method returns a random value that is greater than or equal to 0.0 and less than 1.0.
Example
Syntax of Math.random()
The syntax of the random() method is:
Math.random()
Note: The random() method is a static method. Hence, we can call the method directly using the class name Math.
random() Parameters
The Math.random() method does not take any parameters.
random() Return Values
- returns a pseudorandom value between 0.0 and 1.0
Note: The values returned are not truly random. Instead values are generated by a definite computational process that satisfies some condition of randomness. Hence called pseudo random values.
Example 1: Java Math.random()
In the above example, we can see that the random() method returns three different values.
Example 2: Generate Random Number Between 10 and 20
Output
Random Numbers between 10 and 20: 15, 13, 11, 17, 20, 11, 17, 20, 14, 14,
Example 3: Access Random Array Elements
Output
Random Array Elements: 67, 34, 77, 34, 12, 77,