▚ 01 Random.nextInt(n)
-
Random.nextInt(n)
方法,生成一个随机的int值,介于[0,n)
的区间,也即包含0而不包含n。
🍎示例程序为:
import java.io.*;
import java.util.Random;
public class randomInt {
public static void main(String[] args) throws IOException{
Random r = new Random();
int num = 10;
// n1介于[0, 10)
int n1 = r.nextInt(num);
System.out.println("n1 = " + n1);
//n2 介于 [-5, 5)
int n2 = r.nextInt(num) - 5;
System.out.println("n2 = " + n2);
}
}
🍏运行结果为:
▚ 02 Random.nextDouble()
-
Random.nextDouble()
方法会从该随机数生成器的序列返回下一个伪随机数,其值均匀分布在0.0到1.0之间。
🍎示例程序为:
import java.io.*;
import java.util.Random;
public class randomDouble {
public static void main(String[] args) throws IOException{
Random r = new Random();
System.out.println("random double number = " + r.nextDouble());
}
}
🍏运行结果为: