DarkmoonYZW
2019-09-27 20:24
采纳率: 100%
浏览 216

最终结果有部分重复,是随机数的问题吗? 如果是有解决方法吗?

最终结果有部分重复,是随机数的问题吗?
如果是有解决方法吗?

#include
#include
#include
using namespace std;
int main()
{
int Z = 0;
while (Z != 10000)
{
int i = 0,R=0;
int xa = 1, ya = 16, xb = 16, yb = 1;
int Xstep, Ystep;
while (R != 1)
{
i = i + 1;
struct timeb timeSeed;
ftime(&timeSeed);
srand(timeSeed.time * 1000 + timeSeed.millitm); // milli time
unsigned int crand = rand();
Xstep = rand() % 3;
Ystep = rand() % 3;
switch (Xstep)
{
case 0:xa = xa + 1;break;
case 1:xa = xa - 1;break;
case 2:ya = ya + 1;break;
case 3:ya = ya - 1;break;
}
switch (Ystep)
{
case 0:xb = xb + 1;break;
case 1:xb = xb - 1;break;
case 2:yb = yb + 1;break;
case 3:yb = yb - 1;break;
}
switch (xa)
{
case 17:xa = xa - 16;break;
case 0:xa = xa + 16;break;
}
switch (ya)
{
case 17:ya = ya - 16;break;
case 0:ya = ya + 16;break;
}
switch (xb)
{
case 17:xb = xb - 16;break;
case 0:xb = xb + 16;break;
}
switch (yb)
{
case 17:yb = yb - 16;break;
case 0:yb = yb + 16;break;
}
if (xa == xb && ya == yb)
{
R = R + 1;
cout << "运行次数=" << i << endl;
}
}
Z = Z + 1;
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐