|
| 1 | +package class02; |
| 2 | + |
| 3 | +public class Code02_RandToRand { |
| 4 | + |
| 5 | + // 此函数只能用,不能修改 |
| 6 | + // 等概率返回1~5 |
| 7 | + public static int f() { |
| 8 | + return (int) (Math.random() * 5) + 1; |
| 9 | + } |
| 10 | + |
| 11 | + // 等概率得到0和1 |
| 12 | + public static int a() { |
| 13 | + int ans = 0; |
| 14 | + do { |
| 15 | + ans = f(); |
| 16 | + } while (ans == 3); |
| 17 | + return ans < 3 ? 0 : 1; |
| 18 | + } |
| 19 | + |
| 20 | + // 等概率返回0~6 |
| 21 | + public static int b() { |
| 22 | + int ans = 0; |
| 23 | + do { |
| 24 | + ans = (a() << 2) + (a() << 1) + a(); |
| 25 | + } while (ans == 7); |
| 26 | + return ans; |
| 27 | + } |
| 28 | + |
| 29 | + // 等概率返回1~7 |
| 30 | + public static int c() { |
| 31 | + return b() + 1; |
| 32 | + } |
| 33 | + |
| 34 | + // 这个结构是唯一的随机机制 |
| 35 | + // 你只能初始化并使用,不可修改 |
| 36 | + public static class RandomBox { |
| 37 | + private final int min; |
| 38 | + private final int max; |
| 39 | + |
| 40 | + // 初始化时请一定不要让mi==ma |
| 41 | + public RandomBox(int mi, int ma) { |
| 42 | + min = mi; |
| 43 | + max = ma; |
| 44 | + } |
| 45 | + |
| 46 | + // 13 ~ 17 |
| 47 | + // 13 + [0,4] |
| 48 | + public int random() { |
| 49 | + return min + (int) (Math.random() * (max - min + 1)); |
| 50 | + } |
| 51 | + |
| 52 | + public int min() { |
| 53 | + return min; |
| 54 | + } |
| 55 | + |
| 56 | + public int max() { |
| 57 | + return max; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // 利用条件RandomBox,如何等概率返回0和1 |
| 62 | + public static int rand01(RandomBox randomBox) { |
| 63 | + int min = randomBox.min(); |
| 64 | + int max = randomBox.max(); |
| 65 | + // min ~ max |
| 66 | + int size = max - min + 1; |
| 67 | + // size是不是奇数,odd 奇数 |
| 68 | + boolean odd = (size & 1) != 0; |
| 69 | + int mid = size / 2; |
| 70 | + int ans = 0; |
| 71 | + do { |
| 72 | + ans = randomBox.random() - min; |
| 73 | + } while (odd && ans == mid); |
| 74 | + return ans < mid ? 0 : 1; |
| 75 | + } |
| 76 | + |
| 77 | + // 给你一个RandomBox,这是唯一能借助的随机机制 |
| 78 | + // 等概率返回from~to范围上任何一个数 |
| 79 | + // 要求from<=to |
| 80 | + public static int random(RandomBox randomBox, int from, int to) { |
| 81 | + if (from == to) { |
| 82 | + return from; |
| 83 | + } |
| 84 | + // 3 ~ 9 |
| 85 | + // 0 ~ 6 |
| 86 | + // 0 ~ range |
| 87 | + int range = to - from; |
| 88 | + int num = 1; |
| 89 | + // 求0~range需要几个2进制位 |
| 90 | + while ((1 << num) - 1 < range) { |
| 91 | + num++; |
| 92 | + } |
| 93 | + |
| 94 | + // 我们一共需要num位 |
| 95 | + // 最终的累加和,首先+0位上是1还是0,1位上是1还是0,2位上是1还是0... |
| 96 | + int ans = 0; |
| 97 | + do { |
| 98 | + ans = 0; |
| 99 | + for (int i = 0; i < num; i++) { |
| 100 | + ans |= (rand01(randomBox) << i); |
| 101 | + } |
| 102 | + } while (ans > range); |
| 103 | + return ans + from; |
| 104 | + } |
| 105 | + |
| 106 | + public static void main(String[] args) { |
| 107 | + |
| 108 | + int[] count = new int[8]; // 0 1 2 .. 7 |
| 109 | + int testTime = 10000000; |
| 110 | + for (int i = 0; i < testTime; i++) { |
| 111 | + int ans = c(); |
| 112 | + count[ans]++; |
| 113 | + } |
| 114 | + |
| 115 | + for (int i = 0; i < 8; i++) { |
| 116 | + System.out.println(i + " : " + count[i]); |
| 117 | + } |
| 118 | + |
| 119 | +// RandomBox randomBox = new RandomBox(3, 9); |
| 120 | +// int from = 17; |
| 121 | +// int to = 29; |
| 122 | +// int[] ans = new int[to + 1]; |
| 123 | +// int testTime1 = 1000000; |
| 124 | +// for (int i = 0; i < testTime1; i++) { |
| 125 | +// ans[random(randomBox, from, to)]++; |
| 126 | +// } |
| 127 | +// for (int i = 0; i < ans.length; i++) { |
| 128 | +// System.out.println(ans[i]); |
| 129 | +// } |
| 130 | +// System.out.println("=========="); |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments