开发者

Getting a random number between 0 and 0.06 in Java?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜