# How to use random numbers in Java?

There are two common ways to generate random numbers in Java: by using the java.util.Random class or by using the Math.random() method.

- random number generator in Java

```
import java.util.Random;
Random random = new Random();
// 生成一个范围在0到100之间的随机整数
int randomInt = random.nextInt(101);
// 生成一个范围在0.0到1.0之间的随机浮点数
double randomDouble = random.nextDouble();
```

- Generate a random number using math.

```
// 生成一个范围在0.0到1.0之间的随机浮点数
double randomDouble = Math.random();
// 生成一个范围在0到100之间的随机整数
int randomInt = (int) (Math.random() * 101);
```

It is important to note that the Random class offers more flexibility and control, making it recommended for more complex random number generation needs. On the other hand, the Math.random() method is simpler and more convenient, suitable for simple random number generation scenarios.