目录
随机函数
Math.random(); ->double -> [0,1) 等概率
public class Test {
public static void main(String[] args) {
int testTimes = 100000;
int count = 0;
for (int i = 0; i < testTimes; i++) {
if (Math.random() < 0.3) {
count++;
}
}
System.out.println((double) count / (double) testTimes);
System.out.println("===============");
// [0,1) -> [0,8)
count = 0;
for (int i = 0; i < testTimes; i++) {
if (Math.random() * 8 < 5) {
count++;
}
}
System.out.println((double) count / (double) testTimes);
System.out.println((double) 5 / (double) 8);
System.out.println("===============");
int k = 5;
// (0,1] -> (0,k-1)
int ans = (int) (Math.random() * k);
System.out.println(ans);
System.out.println("===============");
//等概率
int K = 9;
int[] counts = new int[K];
for (int i = 0; i < testTimes; i++) {
int a = (int) (Math.random() * K);
counts[a]++;
}
for (int i = 0; i < counts.length; i++) {
System.out.println("i 这个数 出现了 " + counts[i] + " 次");
}
System.out.println("===============");
count = 0;
double x = 0.2;
for (int i = 0; i < testTimes; i++) {
if (xToXPower2() < x) {
count++;
}
}
System.out.println((double) count / (double) testTimes);
System.out.println(Math.pow(x, 2));
}
//返回[0,1)的一个小数
//任意的X,X属于[0,1),[0,X)范围上的数出现概率由原来的X调整成x平方
public static double xToXPower2() {
return Math.max(Math.random(), Math.random());
}
}
已知从1-5随机 函数 如何 返回 1-7 随机
public class Test {
public static void main(String[] args) {
int testTimes = 100000;
int[] counts = new int[9];
for (int i = 0; i < testTimes; i++) {
counts[g()]++;
}
for (int i = 0; i < counts.length; i++) {
System.out.println(i + " 出现了" + counts[i] + " 次");
}
}
// 1-5 等概率返回 黑盒
public static int f1() {
return (int) (Math.random() * 5) + 1;
}
//将 f1 修改 为 0 1 发生器 等概率返回 0 1
public static int f2() {
int ans = 0;
do {
ans = f1();
} while (ans == 3);
return ans < 3 ? 0 : 1;
}
// 加工为 0 - 7 等概率返回 思想 二进制位 000 - 111 为 0 - 7
public static int f3() {
return (f2() << 2) + (f2() << 1) + (f2() << 0);
}
// 将 0 出现的概率 均分, 遇到 0 就重做
public static int g() {
int ans = 0;
do {
ans = f3();
} while (ans == 0);
return ans;
}
}
0 1不等概率随机到 0 1等概率随机
public class Test {
public static void main(String[] args) {
int testTimes = 100000;
int[] counts = new int[9];
for (int i = 0; i < testTimes; i++) {
counts[y()]++;
}
for (int i = 0; i < counts.length; i++) {
System.out.println(i + " 出现了" + counts[i] + " 次");
}
}
//你只能知道,X会以固定概率返回0和1,但是x的内容,你看不到!
public static int x() {
return Math.random() < 0.8 ? 0 : 1;
}
/**
* 等概率 返回 0 和 1
* 比如: 不等概率 0 的概率 80% 1 的概率是 20%
* 那么 两次 roll 出
* 0 0 的概率是 80% * 80% = 64%
* 0 1 的概率是 80% * 20% = 16%
* 1 0 的 概率是 80% * 20% = 16%
* 1 1 的概率是 20% * 20% = 4 %
* 总和是 100% , 01 和 10 的概率都是 16% 所以是等概率
*/
public static int y() {
int ans = 0;
do {
ans = x();
} while (ans == x());
// ans = 0 1
// ans = 1 0
return ans;
}
}
对数器
// 采用大样本量 对你的程序逻辑 进行验证
public class Test {
public static void main(String[] args) {
int maxLen = 5;
int maxValue = 1000;
int testTimes = 100000;
for (int i = 0; i < testTimes; i++) {
int[] arr1 = lenRandomValueRandom(maxLen, maxValue);
int[] arr2 = copyArray(arr1);
//执行你的逻辑 在进行判断 TODO
}
}
// 返回一个数组arr,arr长度[o, maxlen-1],arr中的每个值[ 0 , maxvalue-1 ]
public static int[] lenRandomValueRandom(int maxLen, int maxValue) {
int len = (int) (Math.random() * maxLen);
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
arr[i] = (int) (Math.random() * maxValue);
}
return arr;
}
public static int[] copyArray(int[] arr) {
int[] newArr = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
return newArr;
}
// arr1 和 arr2 等长
public static boolean equalValues(int[] arr1, int[] arr2) {
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
}