『壹』 java中的random.nextInt(33)問題
隨機產生一個大於等於0,小於33的整形數
『貳』 怎麼在java的用random的nextint產生一個0-100的數
Randomrand=newRandom();
//包括0但不包括100,要包括100.請寫成nextInt(101)
inti=rand.nextInt(100);
}
『叄』 java nextInt()方法
nextInt()函數說明:
如想得到30到200的(包含30和200)這個跨度的數在java中一般可以有如下方式獲得:
(1)int
i
=
(int)(Math.random()*171)
+
30;
(2)Random
r
=
new
Random
()
;
r.nextInt
(201)
;
//
這個是0
-
200
(3)Random
r
=
new
Random
()
;
r.nextInt
(171)
+
30
;
//
這個是30
到
200.
//如下為二維數組的一點兒東西
。
public
class
數組的使用說明代碼
{
public
static
void
main(String
args[]){
int[]
array=creatArray(10);
printArray(array);
}
public
static
int[]
creatArray(int
length){
//構造含length個元素的數組的方法
int[]
array
=new
int[length];
Random
rad=new
Random();
//產生隨機數的方法(系統自己的)
for(int
i=0;i<array.length;i++){
int
value
=
rad.nextInt(100)
+
200;
//rad.nextInt(100)
意思是隨機產生一個大於等於0小於100的數
------即包含0不包含100
array[i]=value;
}
return
array;
}
public
static
void
printArray(int[]
array){
for(int
i=0;i<array.length;i++)
System.out.println(array[i]+'\t');
}
『肆』 一個關於JAVA裡面random.nextInt()的問題
從java的源代碼里可以看到:
/*
* @return the next pseudorandom, uniformly distributed {@code int}
* value from this random number generator's sequence
*/
public int nextInt() {
return next(32);
}
它調用了next(int)方法, 返回的是32位的隨機序列值
『伍』 java中new random().nextin()是什麼意思
Random random = new Random();
//隨機獲取int范圍內的一個數
random.nextInt();
//0-10的隨機數
random.nextInt(10)
『陸』 java.util.Random類的.nextInt()方法返回的結果
不包括MAX
建議你下載一本JAVA API文檔
Random nextInt
public int nextInt(int n)返回一個偽隨機數,它是取自此隨機數生成器序列的、在 0(包括)和指定值(不包括)之間均勻分布的 int 值。
源碼如下:
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;
}
『柒』 java.util.Random的nextInt( )方法會生成一個正整數類型的偽隨機數。 這句話怎麼錯了
自然數類型的隨機數。