2011-10-19 56 views

回答

29

取決於你如何接近類似的東西的問題:

public int gen() { 
    Random r = new Random(System.currentTimeMillis()); 
    return 10000 + r.nextInt(20000); 
} 

或者類似的東西(你可能想的方法隨機對象的instantation但我只是把它放在這裏爲簡便起見):

public int gen() { 
    Random r = new Random(System.currentTimeMillis()); 
    return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000)); 
} 

的想法是,1 + nextInt(2)應始終給予1或2。然後,通過10000相乘,以滿足您的要求,然後添加一些betwee n [0..9999]。

下面的是一些示例輸出:

14499 
12713 
14192 
13381 
14501 
24695 
18802 
25942 
21558 
26100 
29350 
23976 
29045 
16170 
23200 
23098 
20465 
23284 
16035 
18628 
+0

非常感謝你 – Binyomin

+1

@Binyomin如果答案是有幫助的點擊那個向上的箭頭上面的大爲零。 – david