📅  最后修改于: 2023-12-03 14:44:10.546000             🧑  作者: Mango
Math.random()
is a method in Java that generates a pseudorandom double value between 0.0 and 1.0. It is a part of the java.lang
package and can be used in any Java program.
The syntax of Math.random()
method is as follows:
public static double random()
// generate a random number between 0.0 and 1.0
double randomValue = Math.random();
System.out.println(randomValue);
The output will be a random double value between 0.0 and 1.0.
// use Math.random() to generate a random number between a given range
int min = 1;
int max = 100;
int randomNum = (int) (Math.random() * (max - min + 1)) + min;
System.out.println(randomNum);
This program generates a random integer between 1 and 100.
Math.random()
generates a random double value between 0.0 and 1.0.Math.random()
with the difference between the max
and min
value, add min
, and convert the result to an integer using typecasting.Math.random()
is a useful method in Java for generating random numbers. It is easy to use and can be used in various applications, such as generating a random password, generating a random name, or creating random data for testing purposes.