开发者

setting the lower limit for random number function

How to set the lower limit for this

<%= (int) (Math.random() * 60) %>

Now it give number from 0 to 60 if i want number 开发者_高级运维in between 50 to 60 so how to set it. Thanks


    <%= (int) (Math.random() * 10 + 50) %>

That is,

   <%= (int) (Math.random() * (Max-Min) + Min) %>


Better use java.util.Random.

Random random = new Random();
// ...
int i = 50 + random.nextInt(11);

(note that nextInt() is exclusive)


Unrelated to the concrete problem, Java code should be placed in Java classes rather than JSP files.


(Math.random() * 10) will give you a random number between 0 and 10. I'll leave it up to you to figure out how to translate that to between 50 and 60...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜