生成一个范围内的随机双精度 [英] Generate a random double in a range

查看:28
本文介绍了生成一个范围内的随机双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个像下面这样的双打

I have two doubles like the following

double min = 100;
double max = 101;

使用随机生成器,我需要在最小值和最大值之间创建一个双精度值.

and with a random generator, I need to create a double value between the range of min and max.

Random r = new Random();
r.nextDouble();

但是这里没有任何东西可以指定范围.

but there is nothing here where we can specify the range.

推荐答案

要在 rangeMinrangeMax 之间生成一个随机值:

To generate a random value between rangeMin and rangeMax:

Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();

这篇关于生成一个范围内的随机双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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