Random.nextFloat不适用于浮点数吗? [英] Random.nextFloat is not applicable for floats?

查看:83
本文介绍了Random.nextFloat不适用于浮点数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

float minX = 50.0f;
float maxX = 100.0f;

Random rand = new Random();

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

"Random类型的nextFloat()方法不适用于参数(float)"

"The method nextFloat() in the type Random is not applicable for the arguments (float)"

嗯,什么?

推荐答案

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;

这篇关于Random.nextFloat不适用于浮点数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆