0 到 0.06 之间的 Java 随机数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15940862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java random number between 0 and 0.06
提问by Brittany Gefroh
In Java, I need to randomly generate a number between 0
and 0.06
.
在 Java 中,我需要在0
和之间随机生成一个数字0.06
。
I saw on another question on here that the following code would do the trick:
我在这里的另一个问题上看到以下代码可以解决问题:
Random generator = new Random();
double number = generator.nextDouble() * .06;
However, doing that gives me really long numbers like 0.007044013589130205
and 0.03656588431980957
, which I don't think is what my instructor is looking for.
然而,这样做给了我很长的数字,比如0.007044013589130205
和0.03656588431980957
,我认为这不是我的导师正在寻找的。
Is there anyway to generate random numbers between 0 and 0.06 that only have two or three decimal places?
无论如何要生成0到0.06之间只有两个或三个小数位的随机数?
回答by dreamcrash
You can do it this way, if you want you number to range between [0;0.06] with 0.01 of difference:
你可以这样做,如果你想让你的数字在 [0;0.06] 之间有 0.01 的差异:
Random generator = new Random();
int number = generator.nextInt(7);
double result = number / 100.0;
You can generalize the formula number = generator.nextInt(max + 1 -min) + min;
where max will be the biggest number that you what to generate and min the lowest. This will generate number from [min to max+1]
您可以概括公式number = generator.nextInt(max + 1 -min) + min;
,其中 max 将是您要生成的最大数字,而 min 将是最低数字。这将从[min to max+1]
To increase the decimal places you just have to divide number for 1000, 10000 and so on.
要增加小数位数,您只需将数字除以 1000、10000 等。
回答by Ted Hopp
Internally, Java double
values are a binary floating point representation. They don't have decimal places. You can formata double as a base-10 number string having a specified number of decimal places. For example, to get three decimals (precision 0.001), you could use:
在内部,Javadouble
值是二进制浮点表示。他们没有小数位。您可以将 double格式化为具有指定小数位数的 base-10 数字字符串。例如,要获得三位小数(精度 0.001),您可以使用:
String formatted = String.format("%.3f", number);
If you don't need to do any calculations with the number and you want two or three decimal places, just print 0.0
and then a two-digit random integer between 00 and 60 (inclusive).
如果您不需要对数字进行任何计算,并且想要两位或三位小数,只需打印0.0
然后打印一个介于 00 和 60(含)之间的两位数随机整数。
回答by BlackJoker
I assume you need precision to be 0.001, so try this:
我假设您需要精度为0.001,所以试试这个:
Random r = new Random();
int ret = r.nextInt(60 + 1);
return ret / 1000.0;
this will generate [0,0.060] averagelywith difference multiple of 0.001,that may be your want
这将平均生成 [0,0.060] ,差异倍数为 0.001,这可能是您想要的
回答by Mike Samuel
Since you're dealing with sales tax, you should be aware of Why not use Double or Float to represent currency?and Using BigDecimal to work with currencies
既然你在处理销售税,你应该知道为什么不使用 Double 或 Float 来表示货币?和使用 BigDecimal 处理货币
To generate a random BigDecimal
with constraints on range and # of significant digits, see How can I create a random BigDecimal in Java?
要生成具有BigDecimal
范围和有效数字数限制的随机数,请参阅如何在 Java 中创建随机 BigDecimal?