rand.nextint()
生活随笔
收集整理的這篇文章主要介紹了
rand.nextint()
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
自從JDK最初版本發布起,我們就可以使用java.util.Random類產生隨機數了。在JDK1.2中,Random類有了一個名為nextInt()的方法:
public int nextInt(int n)
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
public int nextInt(int n)
給定一個參數n,nextInt(n)將返回一個大于等于0小于n的隨機數,即:0 <= nextInt(n) < n。?
/*** Returns a pseudo-random uniformly distributed {@code int}* in the half-open range [0, n).*/public int nextInt(int n) {if (n <= 0) {throw new IllegalArgumentException("n <= 0: " + n);}if ((n & -n) == n) {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;}創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的rand.nextint()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 共享移动硬盘,随时登陆上QQ
- 下一篇: 2018双一流排名 计算机,2018中国