读书人

[]红球不能产生10以内随机号码[C语言]

发布时间: 2013-07-09 09:50:47 作者: rapoo

[求助]红球不能产生10以内随机号码[C语言]
自己写了一个随机生成双色球号码的小程序,但是很奇怪,运行了很多次,红球全都是10以上的数字,百思不得解,求达人指点迷津

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define RANGE 33
#define BALLS 6

int main()
{
int numbers[RANGE];
int red[BALLS];
int a,b,c,ball,temp;
puts("L O T T O P I C K E R\n");
srand((unsigned)time(NULL));

/*initialize the array*/

for(c<0;c<RANGE;c++)
numbers[c]=0;
printf("Press Enter to pick this week's numbers:");
getchar();

/*draw the numbers*/

puts("Here they come:");
for(c=0;c<BALLS;c++)
{

/*see if a number has already been drawn*/

do
{
ball=rand()%RANGE;
}
while(numbers[ball]);

/*number drawn*/

numbers[ball]=1;
red[c]=ball+1;
}

/*Sort the red balls*/

for(a=0;a<BALLS-1;a++)
for(b=a+1;b<BALLS;b++)
if(red[a]>red[b])
{
temp=red[b];
red[b]=red[a];
red[a]=temp;
}

for(c=0;c<BALLS;c++)
printf("%2d ",red[c]);

/*add the blue ball*/

printf("+ %d",rand()%16+1);
printf("\n\nGood luck in the drawing!\n");
return(0);
}
C
[解决办法]
程序执行过程中没有挂掉么?
    for(c<0;c<RANGE;c++)
numbers[c]=0;

[解决办法]
for(c<0;c<RANGE;c++)

[解决办法]
看错了,原来你有初始化numbers的代码,但是:

for(c<0;c<RANGE;c++)//c<0改为c=0
numbers[c]=0;




rand函数是精心设计过的,选择的计算参数要尽可能保证在范围为0..RAND_MAX时有着最好的统计特性,你一模RANGE,就把这些设计给浪费了。

用ball = (int)((double)rand()/(RAND_MAX + 1)* RANGE);的方法可以让你的ball在0..RANGE-1范围内的统计特性相似于rand()在0..RAND_MAX范围内的表现

读书人网 >C语言

热点推荐