Random.nextFloat is not applicable for floats?

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

    The nextFloat method doesn't take an argument. Call it, then scale the returned value over the range you want.

    float minX = 50.0f;
    float maxX = 100.0f;
    
    Random rand = new Random();
    
    float finalX = rand.nextFloat() * (maxX - minX) + minX;
    

提交回复
热议问题