gpt4 book ai didi

java - Java 中的随机数问题

转载 作者:行者123 更新时间:2023-12-02 11:18:08 25 4
gpt4 key购买 nike

我编写了一段代码,用 Java 随机方向移动对象。有两个功能。

  1. FindRandomDirection - 从 8 个可能的方向中获取随机方向(方向由数字键盘中的数字 1、2、3、4、6、7、8、9 表示)。检查对象是否靠近任何边界。如果是这样,该对象将向远离边界的方向移动。

  2. MoveObject - 通过移动恒定的 STEP 来更改对象的 (X, Y) 坐标。

但是我给 X、Y 的值是多少?重复该过程几次(700或更多)后,X、Y的值变为{X:20-50}和{Y:450-465}。

Case 1: (x:35,y:65) becomes (x:35, y:465)
Case 2: (x:30, y:455) becomes (x:30, y:460)
Case 3: (x:435, y:65) becomes (x:25, y:460)
Case 4: (x:430, y:465) becomes (x:40, y:460)

我想知道为什么在几次迭代之后 x、y 点会收敛到这些值,即使我随机生成数字。

下面是相同的代码。

import java.io.*;
public class bug
{
//public static int x = 35;
//public static int y = 60;

//public static int x = 35;
//public static int y = 460;

//public static int x = 435;
//public static int y = 60;

public static int x = 435;
public static int y = 460;

public static final int NORTH = 8;
public static final int EAST = 6;
public static final int WEST = 4;
public static final int SOUTH = 2;
public static final int NORTHEAST = 9;
public static final int NORTHWEST = 7;
public static final int SOUTHWEST = 1;
public static final int SOUTHEAST = 3;
public static final int STEP = 5;

//Function to move the object in a specified direction.
public static void moveObject(int direction)
{
double nX = 0, nY=0;
switch(direction)
{
case NORTH:
nY = y- STEP;
nX = x;
break;
case SOUTH:
nY = y+ STEP;
nX = x;
break;
case EAST:
nY = y;
nX = x + STEP;
break;
case WEST:
nY = y;
nX = x- STEP;
break;
case NORTHEAST:
nX = x + STEP;
nY = y- STEP;
break;
case NORTHWEST:
nX = x- STEP;
nY = y- STEP;
break;
case SOUTHEAST:
nX = x + STEP;
nY = y+ STEP;
break;
case SOUTHWEST:
nX = x- STEP;
nY = y+ STEP;
break;
}
x = (int) nX;
y = (int) nY;
System.out.println("Direction: "+direction+"; X: "+x+"; Y: "+y);
}
//Function to move the object in a random direction
//Also if wall(Border) is present the object should move in proper direction
public static int findRandomDirection(int objObjectX, int objObjectY)
{
int[] move = {1,2,3,4,0,6,7,8,9};
int randDir=0;
//Generate a random direction to move. Generate new direction if the objected can not be moved in a direction
do
{
java.util.Random randomGenerator = new java.util.Random();
randDir = randomGenerator.nextInt(8);

//If the object lies near East Border, it can not move in that direction
if(objObjectX <= 25)
{
move[0] = 0;
move[3] = 0;
move[6] = 0;
}

//If the object lies near West Border, it can not move in that direction
if(objObjectX >= 465)
{
move[2] = 0;
move[5] = 0;
move[8] = 0;
}

//If the object lies near North Border, it can not move in that direction
if(objObjectY <= 25)
{
move[6] = 0;
move[7] = 0;
move[8] = 0;
}

//If the object lies near South Border, it can not move in that direction
if(objObjectY >= 465)
{
move[0] = 0;
move[1] = 0;
move[2] = 0;
}
} while(move[randDir]==0);
return move[randDir];
}
public static void main(String[] args)
{
for(int i = 0; i<1000;i++)
{
int dir=findRandomDirection(x,y);
moveObject(dir);
}
}
}

随着时间的推移,我的物体会移向棋盘的左下角。请帮助我找到错误。

最佳答案

由于您使用的是 nextInt(8),因此返回的值将始终在 0 到 7 之间(含)。由于 8 永远不会返回,因此移动会偏向相反的方向。您可能想要使用 nextInt(9) 返回 0 到 8(含)之间的值。

编辑:澄清一下,由于永远不会选择“8”作为随机方向,并且移动[8]==9,因此该对象永远不会在东北中移动方向,这意味着随着时间的推移,它将倾向于向西南移动。

此外,正如 @Joey 上面所说,您不应该每次都重新初始化 Random 对象,但这并不是导致漂移行为的原因。

关于java - Java 中的随机数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9692219/

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