嘿,我正在尝试使用最小毫秒延迟、最大毫秒延迟和一些其他计算来编写随机延迟。为此,我需要限制一些整数和一个长整数。下面是一个我称为 MathUtil 的类,用于尝试编写 Clamp 代码。
public static long clamp(long delayPreClamp, int min, int max) {
return 90;
}
您可以在下面找到它正在使用的数字:
double deviation = 22;
double mean = 90;
int min = 43;
int max = 198;
Random r = new Random();
double randGauss = (r.nextGaussian() * deviation);
long delayPreClamp = Math.round(randGauss + mean);
long delay = (long) MathUtil.clamp(delayPreClamp, min, max);
我的问题是,在第一个提到的代码中,我只能返回 min、max、delayPreClamp 或数字。我需要它来创建一个新的数字作为延迟。
最佳答案
public static long clamp(long delayPreClamp, int min, int max) {
// v = delayPreClamp
// if v < min, returns the greater between min and v, thus min
// if v > max, returns the greater between min and max, thus max
// if v is between min and max, returns the greater between min and v, thus v
return Math.max(min, Math.min(delayPreclamp, max));
}
关于java - 我正在尝试在 java 中创建一个 Clamp 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44436118/