在Java中,可以使用Random
類來生成隨機數。以下是一些示例代碼:
Random random = new Random();
int randomNumber = random.nextInt(100);
System.out.println(randomNumber);
Random random = new Random();
int min = 10;
int max = 20;
int randomNumber = random.nextInt(max - min + 1) + min;
System.out.println(randomNumber);
Random random = new Random();
double randomNumber = random.nextDouble();
System.out.println(randomNumber);
Random random = new Random();
boolean randomBoolean = random.nextBoolean();
System.out.println(randomBoolean);
以上是一些生成隨機數的常用方法,你可以根據具體的需求來選擇合適的方法來生成隨機數。