|
本帖最后由 saidline 于 2013-4-22 00:16 编辑
供大家参考,其中除数可更换为其他,7为一个代表
经检验,times在2000以上,频率才比较靠谱,总体看来随机性是良好的。
[code=Cpp width=740px]#include <stdio.h>
#include <windows.h>
int main()
{ srand(time(0));
int a[7]={0}, times=0;//a[7]存放余数0~7的总出现次数,times总个数
float per[7]={0};//per[7]存放余数0~7的出现频率
while(1){
int num=rand()%7,i;//随机数为0~7,通过统计判断是否平均出现,如果平均,频率应全部 逼近14%
printf("\n%d -- ",num);
a[num]++;times++;
per[num]=a[num]*100/times;
for (i=0;i<7;i++) printf("%2d||%2.0f%% ",a,per);
printf("%3d",times);
Sleep(100);
}
system("pause");
return 0;
}[/code] |
|