gpt4 book ai didi

performance - 如何生成随机 "enough"的随机整数?

转载 作者:行者123 更新时间:2023-12-04 02:43:34 24 4
gpt4 key购买 nike

我正在尝试解决 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 文件,并使用以下 Octave 代码找到步数的预期值;
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(..) 输入进行此操作) .

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

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

最佳答案

你可以使用类似的东西

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

int randNumber = dist(gen);

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

关于performance - 如何生成随机 "enough"的随机整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47591208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com