Random.nextFloat is not applicable for floats?

前端 未结 4 2293
清酒与你
清酒与你 2021-02-13 02:02
float minX = 50.0f;
float maxX = 100.0f;

Random rand = new Random();

float finalX = rand.nextFloat(maxX - minX + 1.0f) + minX;

\"The method nextFloat

4条回答
  •  情歌与酒
    2021-02-13 02:05

    Random only return floats between 0 and 1.0 (no overloaded version like for integers): See the Javadocs here: http://download.oracle.com/javase/6/docs/api/java/util/Random.html#nextFloat()

    I think you can do what you are intending with:

    float finalX = (maxX - minX) * rand.nextFloat() + minX;
    

提交回复
热议问题