classSolutionextendsSolBase{ publicintrand10(){ int ans = 0; do { ans = (rand01() << 3) + (rand01() << 2) + (rand01() << 1) + rand01(); // } while (ans == 15 || ans == 14 || ans == 13 || ans == 12 || ans == 11 || ans == 10); } while (ans > 9); return ans + 1; }
publicintrand01(){ int ans = 0; do { ans = rand7(); } while (ans == 4); return ans < 4 ? 0 : 1; } }
//13 - 17 // 13+ [0,4] publicintrandom(){ return min + (int) (Math.random() * (max - min + 1)); }
publicintrand01(int min, int max){ int size = max - min + 1; // size是奇数还是偶数 boolean odd = (size & 1) != 0; int mid = size / 2; int ans = 0; do { ans = random() - min; } while (odd && ans == mid); return ans < mid ? 0 : 1; }
publicintrand(int min, int max, int from, int to){ if (from == to) { return from; } // 3-9 // 0-6 int range = to - from; int num = 1; //求0-range需要几个2进制位 while ((1 << num) - 1 < range) { num++; }
int ans = 0; do { ans = 0; for (int i = 0; i < num; i++) { ans |= (rand01(min, max) << i); } } while (ans > range); return ans + from; }