
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Math random() method
Description
The Java Math random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random
This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.
Declaration
Following is the declaration for java.lang.Math.random() method
public static double random()
Parameters
NA
Return Value
This method returns a pseudorandom double greater than or equal to 0.0 and less than 1.0.
Exception
NA
Example: Getting Random Number between 0.0 and 1.0
The following example shows the usage of Math random() method to get a random number between 0.0 and 1.0.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get two random double numbers double x = Math.random(); double y = Math.random(); // print the numbers and print the higher one System.out.println("Random number 1:" + x); System.out.println("Random number 2:" + y); System.out.println("Highest number:" + Math.max(x, y)); } }
Output
Let us compile and run the above program, this will produce the following result −
Random number 1:0.5016301836409477 Random number 2:0.591827364722481 Highest number:0.591827364722481
Example: Getting Random Number between 0.0 and 5.0
The following example shows the usage of Math random() method to get a random number between 0.0 and 5.0.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get two random double numbers double x = Math.random() * 5.0; double y = Math.random() * 5.0; // print the numbers and print the higher one System.out.println("Random number 1:" + x); System.out.println("Random number 2:" + y); System.out.println("Highest number:" + Math.max(x, y)); } }
Output
Let us compile and run the above program, this will produce the following result −
Random number 1:2.9497714055410174 Random number 2:1.1016708521940748 Highest number:2.9497714055410174
Example: Getting Random Number between 0.0 and -5.0
The following example shows the usage of Math random() method to get a random number between 0.0 and -5.0.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get two random double numbers double x = Math.random() * -5.0; double y = Math.random() * -5.0; // print the numbers and print the higher one System.out.println("Random number 1:" + x); System.out.println("Random number 2:" + y); System.out.println("Highest number:" + Math.max(x, y)); } }
Output
Let us compile and run the above program, this will produce the following result −
Random number 1:-4.597468918068065 Random number 2:-1.1033485127978726 Highest number:-1.1033485127978726