开发者

Getting a random number between 0 and 0.06 in Java?

开发者 https://www.devze.com 2022-12-19 11:56 出处:网络
How do you get random Double values between 0.0 and 0.06 in Ja开发者_如何学编程va?nextDouble() returns a random floating-point number uniformly distributed between 0 and 1. Simply scale the result as

How do you get random Double values between 0.0 and 0.06 in Ja开发者_如何学编程va?


nextDouble() returns a random floating-point number uniformly distributed between 0 and 1. Simply scale the result as follows:

Random generator = new Random();
double number = generator.nextDouble() * .06;

See this documentation for more examples of Random.


This will give you a random double in the interval [0,0.06):

double r = Math.random()*0.06;


To avoid the inexactness of floating point values you can use a double/integer calculation which is more accurate (at least on x86/x64 platforms)

double d = Math.random() * 6 / 100;


you need to have a look at the Random class


Based on this java doc (though watch the boundary condition):

 new Random().nextDouble() * 0.06
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号