如何生成随机整数随机整数"?

时间:2017-12-01 10:19:37

标签: performance random probability probability-density probability-distribution

我试图解决Project Euler中的280th问题,为此我编写了以下模拟;

#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;
}

并将输出重定向到* .txt文件,并使用以下八度代码找到步数的预期值;

clear
load data.txt
n = data(:,1);

output_precision(15);

mean(n)

%% The actual data

%% milliData1 -> 430.038224000000
%% milliData2 -> 430.031745000000

%% timeTData1 -> 430.029882400000
%% timeTData2 -> 430.019626400000

%% timeUnsigData1 -> 430.028159000000
%% timeUnsigData2 -> 430.009509000000

然而,即使我运行完全相同的代码两次,我也会得到不同的结果,正如您从上面的结果中看到的那样。(注意,我已尝试使用不同的srand(..)输入,原因是我&#39;我要解释一下。)

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

所以我的第一个问题是,我是如何生成随机整数的方法的问题?如果是这样,我们怎样才能克服这个问题,我的意思是我们怎么能够足够随机地生成整数,以便当我们重复相同的实验很多次时,它们之间的期望值小于我得到的这些结果?

1 个答案:

答案 0 :(得分:1)

您可以使用类似

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

int randNumber = dist(gen);

这会产生更均匀的值分布。