#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <time.h> #include "windows.h" int myRandom(int min, int max) { if (max < min) { return 0; } return (int)(rand() / (float)RAND_MAX *(max - min) + 0.5) + min; } int _tmain(int argc, _TCHAR* argv[]) { srand(time(0)); int runCount = 0; float runsum = 0; while (getchar() == 10) { runCount++; int count = 1000;// 循环次数 int prob = 30;// 概率 int apper = 0;// 出现此时 int countTmp = count; while (countTmp--) { int num = myRandom(0, 100); printf("%d,", num); if (num <= prob) { apper++; } } // 统计概率 float result = (float)apper / (float)count; printf("\n小于%d出现的概率: %.2f%\n", prob, result); printf("---------------------------------"); runsum += result; } printf("平均概率:%.2f\n", runsum / runCount); system("pause"); return 0; }
概率生成某个数
最新推荐文章于 2022-03-19 12:53:12 发布