Random的nextInt(int i)方法的返回值

来源:互联网 发布:windows只能进安全模式 编辑:程序博客网 时间:2024/06/15 09:46
new Random().netxInt(int i)
public int nextInt(int n) {        if (n <= 0)            throw new IllegalArgumentException("n must be positive");        if ((n & -n) == n)  // i.e., n is a power of 2            return (int)((n * (long)next(31)) >> 31);        int bits, val;        do {            bits = next(31);            val = bits % n;        } while (bits - val + (n-1) < 0);        return val;    }

docs中说返回值是从0到i,不够准确。
返回值应该是 >=0 , < i,就是说永远也不会返回i。


来源
http://www.blogjava.net/mstar/archive/2005/05/21/5026.html


原创粉丝点击