Performance 如何生成随机整数;够了吗;?

Performance 如何生成随机整数;够了吗;?,performance,random,probability,probability-density,probability-distribution,Performance,Random,Probability,Probability Density,Probability Distribution,我试图在ProjectEuler中解决这个问题,为此我编写了以下模拟 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/time.h> /* Directions 1 2 3 4 */ int grid[5][5] = { {0, 0, 0, 0, 2}, {0, 0, 0, 0, 2}, {0, 0

我试图在ProjectEuler中解决这个问题,为此我编写了以下模拟

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

/* Directions

    1
2       3
    4
*/

int grid[5][5] = {
    {0, 0, 0, 0, 2},
    {0, 0, 0, 0, 2},
    {0, 0, 0, 0, 2},
    {0, 0, 0, 0, 2},
    {0, 0, 0, 0, 2}
};

int InitPos[2] = {2, 2};

int MaxExp = 5000000;

bool Success = false;
int StepCount = 0;
int ExpNumber = 1;
int AntsBag = 0;
void Init();
void CarryFood(int * pos);
void LeftFood(int * pos);
bool checkMovability(int * pos, int direction);
bool moveToDirection(int pos[2], int direction);
bool checkSuccess();
void ShowResult();


int main(int argc, char const *argv[])
{   
    timeval curTime;
    gettimeofday(&curTime, NULL);
    int milli = curTime.tv_usec / 1000;


    time_t t;
    srand((unsigned)time(&t));

    //timeTData*.txt corresponds to using "time(&t)" above 
    //milliData.txt corresponds to using "milli" variable above
    //timeTUnsigData*.txt corresponds to using "(unsigned)time(&t)" above

    printf("%% Experiment Number : %d \n", MaxExp);
    while(ExpNumber <= MaxExp)
    {
        Init();
        int pos[2];
        pos[0] = InitPos[0];
        pos[1] = InitPos[1];
        do{
            int direction = (rand() % 4) + 1;
            if (moveToDirection(pos, direction))
            {
                StepCount++;    
            }
            if (pos[1] == 4&&grid[pos[0]][4]==2&&AntsBag==0)
            {
                CarryFood(pos);
            }
            if (pos[1] == 0&&grid[pos[0]][0]==0&&AntsBag==2)
            {
                LeftFood(pos);
            }
            checkSuccess();
        }
        while(!Success);

        ShowResult();
        ExpNumber++;
    }   

    return 0;
}

void Init()
{
    Success = false;
    StepCount = 0;
    AntsBag = 0;
    int gridInit[5][5] = {
        {0, 0, 0, 0, 2},
        {0, 0, 0, 0, 2},
        {0, 0, 0, 0, 2},
        {0, 0, 0, 0, 2},
        {0, 0, 0, 0, 2}
    };
    for (int i = 0; i < 5; ++i)
    {
        for (int j = 0; j < 5; ++j)
        {
            grid[i][j] = gridInit[i][j];
        }
    }
}

void ShowResult()
{
    /*
    for (int i = 0; i < 5; ++i)
    {
        printf("\n");
        for (int j = 0; j < 5; ++j)
        {
            printf("%d ", grid[i][j]);
        }
    }
    */
    printf("%d %d\n", StepCount, ExpNumber);
}

void CarryFood(int * pos)
{
        AntsBag = 2;
        grid[pos[0]][4] = 0;
}

void LeftFood(int * pos)
{
        AntsBag = 0;
        grid[pos[0]][0] = 2;
}

bool checkMovability(int * pos, int direction)
{
    switch(direction)
    {
        case 1:
        {
            if(pos[1]==0){
                return false;
            }
            break;
        }
        case 2:
        {
            if (pos[0]==0)
            {
                return false;
            }
            break;
        }
        case 3:
        {
            if (pos[0]==4)
            {
                return false;
            }
            break;
        }
        case 4:
        {
            if (pos[1]==4)
            {
                return false;
            }
            break;
        }
        default:
        {
            printf("Wrong direction input is given!!\n");
            return false;
            break;
        }
    }
    return true;

}

bool moveToDirection(int * pos, int direction)
{
    if ( !checkMovability(pos, direction) )
    {
        return false;
    }

    switch(direction){
        case 1:
        {
            pos[1] -= 1;
            break;
        }
        case 2:
        {
            pos[0] -= 1;
            break;
        }
        case 3:
        {
            pos[0] += 1;
            break;
        }
        case 4:
        {
            pos[1] += 1;
            break;
        }
        default:
        {
            printf("I'm stunned!\n");
            return false;
            break;
        }
    }
    return true;
}

bool checkSuccess()
{
    for (int i = 0; i < 5; ++i)
    {
        if (grid[i][0] != 2)
        {
            return false;
        }
    }
    //printf("Success!\n");
    Success = true;
    return true;
}
但是,即使我两次运行完全相同的代码,也会得到不同的结果,正如您从上面的结果中所看到的(请注意,我使用不同的srand(..)输入尝试了这一点,原因我将要解释)

我认为这是因为我如何为蚂蚁的随机方向生成一个1-4之间的随机整数,因为据我所知,这个实验的概率分布应该是相同的,只要我大量重复这个实验(在这个特殊的例子中是5000000次)


所以我的第一个问题是,这真的是我如何生成随机整数的方法的问题吗?如果是这样的话,我们如何克服这个问题,我的意思是,我们如何生成足够多的随机整数,当我们大量重复相同的实验时,它们之间的期望值小于我得到的结果

您可以使用

std::mt19937 gen{std::random_device{}()};
std::uniform_int_distribution<int> dist{1, 4};

int randNumber = dist(gen);
std::mt19937 gen{std::random_device{}()};
std::均匀分布区{1,4};
int randNumber=dist(gen);

这将生成更均匀的值分布。

我怀疑您更多的是通过数学而不是模拟来解决这个问题。构建状态图并对其进行分析以计算平均值可能是可行的。但是,我不确定,输入可能有很大的差异,比如100-1000,所以5米的数据点只会给你显示的意义(比如430.03±0.02);输入的随机性不太可能是主要问题。这是可以解决没有任何随机性,虽然;在“马尔可夫链期望步数”中搜索有关该主题的本科材料。@Veedrac您如何知道输入的差异?那么你是如何计算误差条的呢?我不确定我能在这里的评论中给出一个关于误差条的快速总结,但是(非常)粗略地说,你可以取样本数的平方根来得到相对显著性,然后乘以你的值得到绝对显著性。我不知道我是否手动检查了方差,但是如果你知道随机游动是如何工作的,你可以猜到一些事情。@Veedrac好的,谢谢你的回答。
std::mt19937 gen{std::random_device{}()};
std::uniform_int_distribution<int> dist{1, 4};

int randNumber = dist(gen);