Java中的随机数问题

时间:2012-03-13 20:43:41

标签: java random

我编写了一段代码来在Java中以随机方向移动对象。有两个功能。

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

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

  3. 但是我为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);
            }
        }
    }
    

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

3 个答案:

答案 0 :(得分:6)

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

编辑:为了澄清,因为永远不会选择“8”作为随机方向,而moves[8]==9,对象永远不会在NORTHEAST方向上移动,这意味着随着时间的推移,它将倾向于移动{ {1}}。

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

答案 1 :(得分:3)

选择方向时,可以选择介于0和7之间的值。这些值(在您的映射中)对应于:

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

但请注意,永远将被选中:

public static final int NORTHEAST = 9;

所以,我的代码似乎偏向东南方面并不奇怪......

答案 2 :(得分:1)

nextInt(n)方法将数字从零(包括0)返回到n独占。由于传递的值为8,因此结果来自8个值的集合,0-7。因此,从不选择表示东北方向的数组的第九个元素。

这种对东北的偏见导致最终迁移到西南。