生成Java的随机数的类是java.util.Random
1,生成随机数的方法
➊创建一个随机数对象:Random random = new Random();
➋生成一个1到6之间的随机整数(包括1和6):int x = random.nextInt(6) + 1;
➌生成一个0到1之间的随机小数:double y = random.nextDouble();
➍生成一个随机布尔值:boolean z = random.nextBoolean();
2. 源代码
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
int x = random.nextInt(6) + 1;
double y = random.nextDouble();
boolean z = random.nextBoolean();
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
}
Java教程
![:white_check_mark: :white_check_mark:](https://cdn.silicloud.com/blog-img/blog/img/657d0a7a37434c4406be432e/5-11.png)