当前位置: 代码迷 >> Java相关 >> [求助],随机数问题!!!
  详细解决方案

[求助],随机数问题!!!

热度:418   发布时间:2004-08-12 13:29:00.0
[求助],随机数问题!!!

int n=(int)(Math.random()*m) 如果m=1000000时,这时n 的范围为多大呀? 它可能产生 7 吗 ?

(int)(Math.random())产生的范围是不是[0,1)之间呀?

搜索更多相关的解决方案: 随机数  

----------------解决方案--------------------------------------------------------

There is a gerneral formular to do this, say if we want the interval between a and b(inclusive,b>a), then

int i = (int)(Math.random()*(b-a+1)+a);

more general, we have

int ranNum = (int)(Math.random()*(upperBound-lowerBound+1)+lowerBound);

in the example above, this range should be 0<= n < 1000000, another word 0<= n <= 999999


----------------解决方案--------------------------------------------------------

谢谢老大了


----------------解决方案--------------------------------------------------------

there is a class about random : java.util.Random

you can use it as this : Random rd = new Random();

rd.nextInt(int ceil);

then it will generate a value form 0 to ceil-1.(including 0 and ceil)


----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
学习了。
----------------解决方案--------------------------------------------------------
  相关解决方案