小编典典

未生成新的随机数

java

嗨,当我运行这段代码并返回错误的距离时,不会生成新的随机数。不断产生相同的随机数,从而使我陷入无限循环。有人知道为什么会这样吗?感谢您的光临!

private void setRandomVerticies() 
{
    //generate 200 triangles
    this.randomVerticies = new LinkedList<TGVertex>(); 
    random.setSeed(System.currentTimeMillis());
    short x = 5000, y = 5000;
    int specT = 0;
    int vert = 0;
    int triangleCount = 0;
    int rx = 0, ry = 0;
    int sameAxis = 0;
    TGVertex check = new TGVertex(x,y);
    for (int i = 1; i <= 600; i++)
    {
      for (;;)
      {
        rx = random.nextInt(); 
        ry = random.nextInt();
        System.out.println("generate a new random number please");
        if(i == 1) break;
        if (specT == 1) //approximately 50 triangles will have 2 vertices against the edge
        {
          if       (sameAxis == 0  || sameAxis == 1)  { rx = 0; 
          }else if (sameAxis == 2  || sameAxis == 3)  { ry = 0;
          }else if (sameAxis == 4  || sameAxis == 5)  { rx = tImage.getWidth()-1;
          }else if (sameAxis == 6  || sameAxis == 7)  { ry = tImage.getHeight()-1; }
          check = new TGVertex(rx, ry);
          if (!randomVerticies.contains(check))
          {
            if (vert == 2 && distance(check, randomVerticies.get(i-2)))
            {
              sameAxis++;
              if (sameAxis == 8) sameAxis = 0;
              break; //breaks while loop if new vertex is chosen
            } 
            else if (vert == 1)
            {
              sameAxis++;
              if (sameAxis == 8) sameAxis = 0;
              break; //breaks while loop if new vertex is chosen    
            }
          }
        }
        rx = (short)(rx & 0x1FF);
        ry = (short)(rx & 0x19D);
        check = new TGVertex(rx, ry);
        System.out.println("rx = " + rx + " ry = " + ry);
        System.out.println("check = " + check.getX() + ", " + check.getY());
        System.out.println("randomVert = " + randomVerticies.get(i-2).getX() + ", " + randomVerticies.get(i-2).getY());
        if (!randomVerticies.contains(check) && distance(check, randomVerticies.get(i-2))) break; //breaks while loop if new vertex is chosen
      }

      String sliceX = Integer.toHexString(this.width()-1);
      String sliceY = Integer.toHexString(this.height()-1);
      int sX = (int) Integer.parseInt(sliceX, util.Global.HEX);
      int sY = (int) Integer.parseInt(sliceY, util.Global.HEX);  
      x = (short) (rx & sX); 
      y = (short) (ry & sY);
      //System.out.println("("+x+", "+y+")");
      randomVerticies.add( new TGVertex(x, y) );

      if (i % 3 == 0) triangleCount++;
      if (triangleCount == 2 && vert < 3 )
      { 
        specT = 1;
        vert++;
      } 
      if (vert == 3)
      { 
        specT = 0;
        vert = 0;
        triangleCount = 0;
      }
    }
}

这是输出,如您所见,randomVert从不更改值。

rx = 196 ry = 132
check = 196, 132
randomVert = 225, 392
generate a new random number please
rx = 383 ry = 285
check = 383, 285
randomVert = 225, 392
generate a new random number please
rx = 328 ry = 264
check = 328, 264
randomVert = 225, 392
generate a new random number please
rx = 102 ry = 4
check = 102, 4
randomVert = 225, 392
generate a new random number please
rx = 197 ry = 133
check = 197, 133
randomVert = 225, 392
generate a new random number please
rx = 62 ry = 28
check = 62, 28
randomVert = 225, 392
generate a new random number please
rx = 295 ry = 261
check = 295, 261
randomVert = 225, 392
generate a new random number please
rx = 142 ry = 140
check = 142, 140
randomVert = 225, 392
generate a new random number please
rx = 261 ry = 261
check = 261, 261
randomVert = 225, 392

阅读 249

收藏
2020-11-30

共1个答案

小编典典

您正在Random循环中创建背对背的新实例。只需创建一个(在循环之外!),然后在需要新值时询问它随机数。认为它就像在挖一口井-
每次要喝一杯水都不会挖新的井,而是挖一口井,然后根据需要喝多口水。

2020-11-30