![用01随机函数构造[a,b]整数范围随机数 用01随机函数构造[a,b]整数范围随机数](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
#include <stdio.h>
#include <stdlib.h>
#define RAND_0_1 (rand()&0x1) int random(int a, int b); int main(){
int s[]; for (int i=; i<; i++) {
s[i] = ;
} for (int i=; i<; i++) {
s[random(, )]++;
} for (int i=; i<; i++) printf("%d\n", s[i]); system("pause");
return ;
} int random(int a, int b) {
int d, D = b - a;
int ret = ;
do {
ret = ;
d = b - a;
while (d) {
ret = ret<<;
ret |= RAND_0_1;
d = d>>;
}
} while(ret > D);
return ret + a;
}
算法导论(第二版)里第五章有关于用1/0随机函数产生[a, b]内的随机数,不知这样实现是否正确,运行结果不理想