目标:生成5个双色球号码
程序:
void CPrinter::create_num_array(char * num_array)
{
int tmp = 0;
char tmp_str[3]={0};
int select_num[6]={0};
bool been_select = false;
strncat(num_array,"红球:",6);
for(int i = 0;i<12;i++)
{
if(0 == i%2)
{
do
{
been_select = false;
tmp = 1+rand()%(33-1);
for(int j=0;j<6;j++)
{
if(tmp == select_num[j])
{
been_select = true;
}
}
}while(true == been_select);
select_num[i/2]=tmp;
sprintf(tmp_str,"%d",tmp);
strncat(num_array,tmp_str,strlen(tmp_str));
}
else
{
strncat(num_array," ",1);
}
}
strncat(num_array,"\n蓝球:",strlen("\b蓝球"));
tmp = 1+rand()%(16-1);
sprintf(tmp_str,"%d\n",tmp);
strncat(num_array,tmp_str,strlen(tmp_str));
}
/*主函数中调用:create_num_array:*/
srand((unsigned)time(NULL));
for(k =0;k<bet_num;k++)
{
pt->create_num_array(text);
pt->PrintString(text);
printf("%d\n",k);
memset(text,0,sizeof(text));
}
结果打印出超过bet_num组号码。跟踪K的值,发现不是0,1,2,3。。。递增而是0,1,2,0,1....不规律增长.屏蔽rand函数,赋常值不回出现此问题。
求达人解救
[ 此帖被光子vs电子在2012-02-07 09:40重新编辑 ]