您的位置:首页 > 其它

2013-11-01 实验之足球比赛夺冠模拟(概率公平原理)

2013-11-01 21:24 357 查看
题目:    足球比赛具有一定程度的偶然性,弱队也有战胜强队的可能。

 假设有甲、乙、丙、丁四个球队。根据他们过去比赛的成绩,得出每个队与另一个队对阵时取胜的概率表:
    甲  乙  丙  丁   

甲   -  0.1 0.3 0.5

乙 0.9  -   0.7 0.4 

丙 0.7  0.3 -   0.2

丁 0.5  0.6 0.8 -

    数据含义:甲对乙的取胜概率为0.1,丙对乙的胜率为0.3,...

    现在要举行一次锦标赛。双方抽签,分两个组比,获胜的两个队再争夺冠军。(参见如下)



    请你进行10万次模拟,计算出各队夺冠的概率。

思想:随机数的产生,概率公平原理,有随机数来模拟



应用程序:

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

float teamCrown[4][4] = { {0, 0.1, 0.3, 0.5},
{0.9, 0, 0.7, 0.4 },
{0.7,  0.3, 0, 0.2},
{0.5,  0.6, 0.8,0}};
int counts[4];

int  rand_3()
{
//srand(time(0));
//return rand()%7+1;
struct timeval tpstart;
gettimeofday(&tpstart,NULL);
srand(tpstart.tv_usec);
return 1+(int) (3.0 * rand()/(RAND_MAX+1.0));
}

int  rand_10()
{
struct timeval tpstart;
gettimeofday(&tpstart,NULL);
srand(tpstart.tv_usec);
return 1+(int) (10.0 * rand()/(RAND_MAX+1.0));
}

int  rand_XY(float x)
{
//printf(" value: %f\n", x);
//printf(" value: %d\n", rand_10());
int value = rand_10();
if(value <= x) {
//	printf("i win\n");
return 1;
}else{
//	printf("j win\n");
return 0;
}
}

int do_win_or_lose(int i, int j)
{
float prob = teamCrown[i][j];
if(rand_XY(prob * 10) > 0){
return i;
}else{
return j;
}
}

void choose_win()
{
int oppont = rand_3();
int win;
int i, j;
//printf("Opponet: %d\n", oppont);
if(oppont == 1){
i = do_win_or_lose(0, 1);
j = do_win_or_lose(2, 3);
}else if(oppont == 2){
i = do_win_or_lose(0, 2);
j = do_win_or_lose(1, 3);
}else if(oppont == 3){
i = do_win_or_lose(0, 3);
j = do_win_or_lose(1, 2);
}
//printf("team i j: %d, %d\n", i, j);
counts[do_win_or_lose(i, j)]++;
}

int main(void)
{
int i;
int count = 100000;
for(i = 0; i < count; i++)
{
choose_win();
}
for(i = 0; i < 4; i++)
{
printf("Team %d win probliabity: %d, %f%\n", (i + 1), counts[i], (float)counts[i]/count * 100);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: