在4x4板上模拟随机游走场景

时间:2015-12-04 20:58:52

标签: c++ probability

我正在处理的问题如下。想象一下4x4棋盘,在棋盘上放置一个国王。将王放置在每个正方形上的概率是输入之一(对于所有正方形而言不一定相同)。国王必须进行n次移动(n是输入)。目标是进行一项实验'找到n移动后的概率。

我的朋友通过使用线性代数创建通用公式解决了这个问题。作为对答案的挑战/验证,我想用c ++编写这个程序。

我们都从一个概括开始。那是因为电路板是对称的,我们只考虑国王是在电路板的侧面,角落还是中间正方形。

C S S C

S M M S

S M M S

C S S C

因此,我们只输入P(S_ {initial}),P(C_ {initial}),P(M_ {initial}),国王的移动次数以及实验次数# 39;我们制作。

我的程序代码如下。我的结果不同意我的朋友,所以我通过将n设置为1并将P(S_ {initial}),P(C_ {initial})和P(M_ {initial})设置为1/2来检查我的代码,分别为1/4和1/4。通过条件概率,我知道P(S)= 59/120,P(C)= 21/160,P(M)= 181/481。我的计算不同意,所以我确信我的代码中存在错误或错误,但我无法追踪它。我感谢任何帮助。

此外,我知道我的代码是邋and且低效的,所以我感谢有关如何改进它的评论。谢谢你的帮助。

编辑:输入初始概率我分别为S,C和M输入2,1和1。

EDIT2:原来我使用了" ="而不是运营商" =="在我的if语句中。谢谢@Christopher Oicles。现在一切都按预期工作了。

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <stdio.h>
#include <time.h>

using namespace std;

int S, C, M; //probability 'ratios', i.e. prob of S is S/(S+C+M)
int n; // number of steps
int trials;

int initial(int a,int b,int c); // this 'throws the die' to find where to start and returns 0 for side, 1 for corner, 2 for middle
int step(int a); // this throws the die to make a step and returns the same format
int trial(); //this makes n number of steps, starting at initial(a,b,c) and returns 0,1,2

double data[]={0,0,0}; // the result of the experiment, each element holds the number of times a trial returned the S,C,M where number of s = data[0], I increment this by one every time I run trial();

int main()
{
    srand(time(NULL));

    for(int i=0;i<3;i++) //reset data array
        data[i]=0;

    std::cout << "Enter probability ratio of S:";
    std::cin >> S;
    std::cout << "Enter probability ratio of C:";
    std::cin >> C;
    std::cout << "Enter probability ratio of M:";
    std::cin >> M;
    std::cout << "Enter number of steps per trial:";
    std::cin >> n;
    std::cout << "Enter number of trials:";
    std::cin >> trials;

    for(int i=0;i<trials;i++)
        data[trial()]++;

    for(int i=0;i<3;i++)
        std::cout << data[i]/trials << " \n";

    for(int i=0;i<3;i++)
        std::cout<<data[i]<<" ";

    cout << "\n \n";
}

int trial(){
    int current;//  the current place S, C or M or 0,1,2
    int next;

    current = initial(S,C,M);

    for(int i=0;i<n;i++)
        {next = step(current);
        current = next;}

    return current;
}

int initial(int side,int corner,int middle){
    int t = rand() % (side+corner+middle); //generates a random number from zero to a+b+c-1


    //now we apply the probability
    if(t < side)
        return 0; //side
    if(t >= side && t < (side+corner))
        return 1; //corner
    if(t>=(side+corner))
        return 2; //middle
}

int step(int a){
    int t;

    switch(a){
        case 0: //we are on a side
            {t = rand()%5;
            if(t < 2){return 0;} //2/5 chance to go to a side

            if(t = 2){return 1;} //1/5 chance to go to a corner

            if(t > 2){return 2;} //2/5 chance to go to a middle

            break;}
        case 1: //we are on corner
            {t = rand()%3;
            if(t < 2){return 0;} //2/3 chance to go to side

            if(t = 2){return 2;} //1/3 chance to go to middle

            break;}
        case 2: //we are on middle
            {t = rand()%8;
            if(t < 4){return 0;} //1/2 chance go to side

            if(t = 4){return 1;} //1/8 chance of corner

            if(t > 4){return 2;} //3/8 chance of middle

            break;}
    }
}

0 个答案:

没有答案