Random.nextFloat is not applicable for floats?

前端 未结 4 2438
清酒与你
清酒与你 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:08

    The documentation is very clear, nextFloat() doesn't take any arguments. It'll give you a number between 0 and 1, and you'll need to use that in your calculation.

    edit: example

    private Random random = new Random();
    public float nextFloat(float min, float max)
    {
        return min + random.nextFloat() * (max - min)
    }
    

提交回复
热议问题