直接上源码
/**
* Returns a {@code double} value with a positive sign,
* 返回一个带符号的double类型的数字,说人话就是返回一个非负double类型的数字(包含0)
*
* greater than or equal to {@code 0.0} and less than {@code 1.0}.
* 大于等于0
*
* Returned values are chosen pseudorandomly with (approximately)
* uniform distribution from that range.
* 返回一个【伪随机数】,并且返回的值(近似)【均匀分布】
*
* <p>When this method is first called, it creates a single new
* pseudorandom-number generator, exactly as if by the expression
*
* <blockquote>{@code new java.util.Random()}</blockquote>
*
* This new pseudorandom-number generator is used thereafter for
* all calls to this method and is used nowhere else.
*
* <p>This method is properly synchronized to allow correct use by
* more than one thread. However, if many threads need to generate
* pseudorandom numbers at a great rate, it may reduce contention
* for each thread to have its own pseudorandom-number generator.
*
* @return a pseudorandom {@code double} greater than or equal
* to {@code 0.0} and less than {@code 1.0}.
* @see Random#nextDouble()
*/
public static double random() {
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
核心点
● 返回一个非负double类型数字,返回是[0-1)【包含0不包含1】
● 这是一个伪随机数,真正是随机数应该是没有概率一说
● 产生的没一个随机数的概率近似相等
基础验证:Math.random()随机数等概率
/**
* Math.random();
* 返回一个范围[0-1)的double类型数字(包含0,不包括1)
* 每个数字的返回都是等概率的。
*/
public static void f1() {
int randomCount = 1000_0000;
int count = 0;
double number = 0.1D;
// double number = 0.2D;
// double number = 0.3D;
// double number = 0.4D;
// double number = 0.5D;
// double number = 0.6666D;
for (int i = 0; i < randomCount; i++) {
if (Math.random() < number) {
count++;
}
}
System.out.println("随机小于" + number + "的概率:" + (double) count / (double) randomCount);
}
// 分别测试随机的数字小于0.1、0.2、0.3、0.4、0.5、0.6666的概率结果如下:
// 随机小于0.1的概率:0.1000645
// 随机小于0.2的概率:0.2001116
// 随机小于0.3的概率:0.2999759
// 随机小于0.4的概率:0.3999943
// 随机小于0.5的概率:0.5000803
// 随机小于0.6666的概率:0.6665477
根据以上测试发现,我们想要在[0-1)之间获取一个小于x随机数的概率为≈x%。
再次验证:[0-10)随机数等概率
我们知道Math.random()返回一个[0-1)区间的一个随机数,那么把这个随机数放大10倍,我们就能得到一个[0-10)区间的一个随机数。
/**
* Math.random() * 10;
* 返回一个范围[0-10)的double类型数字(包含0,不包含10)
*/
public static void f2() {
int randomCount = 1000_0000;
// 初始化一个长度为10的数组,每个元素分别记录不同数字产生的个数
// numbers[0] 存储的是 0 的个数
// numbers[1] 存储的是 1 的个数
// ...
// numbers[9] 存储的是 9 的个数
int[] numbers = new int[10];
for (int i = 0; i < randomCount; i++) {
// 生成的随机数放大10倍,并转换成int类型,这个值一定是[0-10)之间的正整数(包含0,不包含10)
numbers[(int) (Math.random() * 10)]++;
}
for (int i = 0; i < numbers.length; i++) {
System.out.println("随机小于" + (i + 1) + "的次数:" + numbers[i] + ", 概率是:" + (double) numbers[i] / (double) randomCount);
}
}
// 测试输出结果如下:
// 随机小于1的次数:999811, 概率是:0.0999811
// 随机小于2的次数:1001029, 概率是:0.1001029
// 随机小于3的次数:1000648, 概率是:0.1000648
// 随机小于4的次数:999331, 概率是:0.0999331
// 随机小于5的次数:1000135, 概率是:0.1000135
// 随机小于6的次数:999298, 概率是:0.0999298
// 随机小于7的次数:998976, 概率是:0.0998976
// 随机小于8的次数:999258, 概率是:0.0999258
// 随机小于9的次数:1001133, 概率是:0.1001133
// 随机小于10的次数:1000381, 概率是:0.1000381
根据以上测试,我们同样能验证想要在[0-10)之间获取一个小于x随机数的概率为≈x%。
问题进阶1:获取一个小于x随机数的概率变成x²%
还是先上代码
public static void f3() {
int randomCount = 1000_0000;
int count = 0;
double number = 0.456D;
for (int i = 0; i < randomCount; i++) {
/**
* 这里执行了2次随机函数,第一次随机小于number的概率为number%,第二次随机小于nunber的概率也为number%
* 那么执行两次都小于number的概率就是number*number%,即number²%。
* 注意:这里取max是为了保证两次获取的数字都是小于指定的数字。换句话说随机多次中取最大的值都不大于指定的数字,那么其他的数字肯定也不大于指定的数字
*/
if (Math.max(Math.random(), Math.random()) < number) {
count++;
}
}
System.out.println("随机小于" + number + "原来的概率:" + number + ", 平方后的概率为:" + Math.pow(number, 2));
System.out.println("随机小于" + number + "的概率:" + (double) count / (double) randomCount);
}
// 分别测试随机的数字小于0.1、0.2、0.3、0.4、0.5、0.6666的概率结果如下:
// 随机小于0.1原来的概率:0.1, 平方后的概率为:0.010000000000000002
// 随机小于0.1的概率:0.0099795
// 随机小于0.2原来的概率:0.2, 平方后的概率为:0.04000000000000001
// 随机小于0.2的概率:0.040024
// 随机小于0.3原来的概率:0.3, 平方后的概率为:0.09
// 随机小于0.3的概率:0.0900515
// 随机小于0.4原来的概率:0.4, 平方后的概率为:0.16000000000000003
// 随机小于0.4的概率:0.1600157
// 随机小于0.5原来的概率:0.5, 平方后的概率为:0.25
// 随机小于0.5的概率:0.2499815
// 随机小于0.6666原来的概率:0.6666, 平方后的概率为:0.44435556
// 随机小于0.6666的概率:0.4446067
代码中我们执行了两次获取随机数,并取最大值。
我们知道执行一次获取到不大于x的概率为x%,那么连续执行两次都小于x的概率就是x% * x%,即为x²%。
那么我们这里为什么要取两次随机数的最大值呢?
如果最大值都比x小,那么另外的值肯定也比x小,所以这里表达的意思就是连续两次随机的数字都小于x的概率。
问题进阶2:获取一个小于x随机数的概率变成x³%
上代码
public static void f4() {
int randomCount = 1000_0000;
int count = 0;
// double number = 0.1D;
// double number = 0.2D;
// double number = 0.3D;
// double number = 0.4D;
// double number = 0.5D;
double number = 0.6666D;
for (int i = 0; i < randomCount; i++) {
/**
* 这里执行了2次随机函数,第一次随机小于number的概率为number%,第二次随机小于nunber的概率也为number%,
* 第三次随机小于nunber的概率也为number%,
* 那么执行两三次都小于number的概率就是number*number%*number%,即number³%。
* 注意:这里取max是为了保证三次获取的数字都是小于指定的数字。换句话说随机多次中取最大的值都不大于指定的数字,那么其他的数字肯定也不大于指定的数字
*/
if (Math.max(Math.random(), Math.max(Math.random(), Math.random())) < number) {
count++;
}
}
System.out.println("随机小于" + number + "原来的概率:" + number + ", 立方后的概率为:" + Math.pow(number, 3));
System.out.println("随机小于" + number + "的概率:" + (double) count / (double) randomCount);
}
// 分别测试随机的数字小于0.1、0.2、0.3、0.4、0.5、0.6666的概率结果如下:
// 随机小于0.1原来的概率:0.1, 立方后的概率为:0.0010000000000000002
// 随机小于0.1的概率:0.001004
// 随机小于0.2原来的概率:0.2, 立方后的概率为:0.008000000000000002
// 随机小于0.2的概率:0.0079951
// 随机小于0.3原来的概率:0.3, 立方后的概率为:0.026999999999999996
// 随机小于0.3的概率:0.0269906
// 随机小于0.4原来的概率:0.4, 立方后的概率为:0.06400000000000002
// 随机小于0.4的概率:0.0641036
// 随机小于0.5原来的概率:0.5, 立方后的概率为:0.125
// 随机小于0.5的概率:0.1250869
// 随机小于0.6666原来的概率:0.6666, 立方后的概率为:0.29620741629599995
// 随机小于0.6666的概率:0.2962848
举一反三:获取一个小于x随机数的概率变成x四次方%
public static void f5() {
int randomCount = 1000_0000;
int count = 0;
double number = 0.456D;
for (int i = 0; i < randomCount; i++) {
/**
* 这里执行了4次随机函数,第一次随机小于number的概率为number%,第二次随机小于nunber的概率也为number%
* 第3次随机小于number的概率为number%,第4次随机小于nunber的概率也为number%
* 那么执行四次都小于number的概率就是number*number%*number%*number%,即number四次方%。
* 注意:这里取max是为了保证多次获取的数字都是小于指定的数字。换句话说随机多次中取最大的值都不大于指定的数字,那么其他的数字肯定也不大于指定的数字
*/
if (Math.max(Math.random(), Math.max(Math.random(), Math.max(Math.random(), Math.random()))) < number) {
count++;
}
}
System.out.println("随机小于" + number + "原来的概率:" + number + ", 四次方后的概率为:" + Math.pow(number, 4));
System.out.println("随机小于" + number + "的概率:" + (double) count / (double) randomCount);
}
以上内容感谢左神。