3,882
社区成员
发帖
与我相关
我的任务
分享
int rand3()
{
int a = rand5();
if(a == 1)
return 1;
else if(a == 2)
return 2;
else if(a == 3)
return 3;
else
return rand3();
}
这个代码也许是对的。
int rand3(){
static int pos=0;
if(pos ==3)pos =0;
pos++;
return ((rand5() +(pos - 1) *5 -1)/ 3 + 1;
}
int rand7(){
static int pos=0;
if(pos ==7)pos =0;
pos++;
return ((rand5() +(pos - 1) *5 -1)/ 7 + 1;
}
这样是否更随机一些。
int rand3(){
static int pos=0;
staic int a[3]={0,2,1}
if(pos ==3)pos =0;
pos++;
return ((rand5() + a [pos -1]*5 -1)/ 3 + 1;
}
int rand7(){
static int pos=0;
staic int a[3]={0,5,2,6,1,4,3};
if(pos ==7)pos =0;
pos++;
return ((rand5() + a[pos - 1] *5 -1)/ 7 + 1;
}
int rand3()
{
int ret;
do
{
ret = rand5();
}while(ret > 3);
return ret;
}
int rand7()
{
int ret;
do
{
ret = rand5() * 5 + rand5();
}while(ret > 21);
return (ret % 7) + 1;
}
int rand7()
{
int a = rand3();
if (a==1)
{
return rand3();
}
if (a==2)
{
return rand3()+3;
}
if (a==3)
{
if (rand3()==1)
return 7;
else
return rand7();
}
}
int rand3(void)
{
int data = 0;
int i;
for(i = 0; i < 3; i++)
data += rand5() - 1;
return (data / 5) + 1;
}