2012-11-27 101 views

回答

1

通過創建Random類的實例。

Random rand = new Random(42); 
int x = rand.nextInt(); // will always be the same 

您還可以使用setSeed方法來(重新)設置一個已經形成的隨機實例的種子:

rand.setSeed(42); 
int x = rand.nextInt(); // will be the same again 
+0

,將這些值是正數或將我必須使用math.abs? – Drixy01

+0

否,它們將是正數,負數或(很小可能性)恰好爲0.但是也有nextInt(int max),它返回0到小於您指定的最大值的整數。 – Philipp