Java中的随机数问题

Syn*_*ter 2 java random

我编写了一段代码来在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)
Run Code Online (Sandbox Code Playgroud)

我想为什么在几次迭代之后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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以随着时间的推移,我的对象移向棋盘的左下角.请帮助我找到错误.

Vee*_*Arr 6

由于您使用nextInt(8),返回的值将始终介于0和7之间(包括).由于8永远不会返回,因此移动偏向相反的方向.您可能希望使用nextInt(9),返回0到8之间的值(包括0和8).

编辑:澄清一下,因为"8"永远不会被选为随机方向,并且moves[8]==9,对象永远不会NORTHEAST朝着方向移动,这意味着随着时间的推移,它将倾向于移动SOUTHWEST.

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