有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在不访问零的情况下根据列表大小范围生成随机数

我有一个蚁群模拟器。这是一个27 x 27的网格,有一个觅食蚂蚁类,它定位食物和最高的信息素水平。我需要在一定范围内随机生成运动

这是一个非常大的项目,所以这里只讨论了方法(如果足够的话):

    private GridNode locateHighestPherms() {
    Random randomNode = new Random();
    LinkedList<GridNode> neighborNodeList = gridLocation.getNeighboringNodes(); //a List of Node Objects that keeps track of adjacent nodes
    LinkedList<GridNode> randomGridNode = new LinkedList<>(); //random destination Node

    for(Iterator<GridNode> gridNodeIterator = neighborNodeList.iterator(); gridNodeIterator.hasNext();) {
        GridNode alreadyVisited = gridNodeIterator.next();
        if(foragerMoveHistory.contains(alreadyVisited) || !alreadyVisited.isVisible()) {
            gridNodeIterator.remove();
        }
    }
    if(neighborNodeList.size() == 0) {
        neighborNodeList = gridLocation.getNeighboringNodes();
    }
    GridNode nodeWithMostPherms = neighborNodeList.get(0);

    for(int checkNode = 1; checkNode < neighborNodeList.size(); checkNode++) {
        if(nodeWithMostPherms.isVisible() && nodeWithMostPherms.getPheromoneUnit() < neighborNodeList.get(checkNode).getPheromoneUnit()) {
            nodeWithMostPherms = neighborNodeList.get(checkNode);
        }
    }
    for (GridNode neighborNode : neighborNodeList) {
        if ((neighborNode.getPheromoneUnit() == nodeWithMostPherms.getPheromoneUnit()) && neighborNode.isVisible()) {
            randomGridNode.add(neighborNode);
        }
    }
    //DEBUGGING
    //System.out.println(randomGridNode.size());
    nodeWithMostPherms = randomGridNode.get(randomNode.nextInt(randomGridNode.size()));
    //nodeWithMostPherms = randomGridNode.get(RandomInstance.randomNumberGen(1, randomGridNode.size()));
    return nodeWithMostPherms;
  }
}

就在那里,^分配给nodeWithMostPherms的是我需要访问下一个随机数的地方。然而,当我最初尝试注释掉的代码时,它崩溃了,因为有时我试图在列表大小为零时访问零

我将向您展示我的RandomInstance类。它又短又甜:

    import java.util.Random;

public class RandomInstance {

static int randomNumber;

public static int randomNumberGen(int lowRange, int highRange) {

    Random numberGenerator = new Random(); //I would prefer not to have this.
    randomNumber = numberGenerator.nextInt(highRange - lowRange + 1) + lowRange;

    /** EXAMPLE FOR REFERENCE
     * setFoodUnitAmount(RandomInstance.randomNumberGen(500, 1000));
     */
    return randomNumber;
  }
}

我之所以有自己的Random类,是因为有许多生成的随机数实例,建议我们创建自己的,这样我们就不会有一堆java实例。util。到处都是

有人对我如何让它适合我的RandomInstance类有什么建议吗? 如果我尝试注释掉的代码,它会抛出IndexOutOfBoundsException

    Exception in thread "Thread-0" java.lang.IndexOutOfBoundsException: Index: 8, Size: 8
at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
at java.util.LinkedList.get(LinkedList.java:476)
at ForagerObject.locateHighestPherms(ForagerObject.java:121)

第121行是上面提到的作业


共 (1) 个答案

  1. # 1 楼答案

    虽然可以共享Random,但不应该共享多个字段

    public class RandomInstance {
        private final Random random = new Random();
    
        public int nextInt(int min, int max) {
            return random.nextInt(max - min + 1) + min;
        }
    }
    

    所以当你叫它

    nodeWithMostPherms = randomGridNode.get(randomInstance.nextInt(1, randomGridNode.size()-1));
    

    或者你可以直接使用Random

    nodeWithMostPherms = randomGridNode.get(random.nextInt(randomGridNode.size()-1)+1);