回答
我不是很熟悉J2ME,但是Javadoc表明Random類是CLDC API的一部分,因此您可以產生這樣一個14位數:
public static void main(String[] args) {
Random r = new Random();
long l = r.nextLong();
System.out.println(String.format("%015d", l).substring(1, 15));
}
我認爲這樣會更少隨機...兩個隨機長可以有相同的前14個字符,而不是等於。 – 2010-01-16 11:42:43
比什麼隨機性更少? – 2010-01-16 11:49:28
如果你做了相反的事情,它只會是「較少隨機」,即嘗試從一個超過10^14個可能值的來源生成一個14位數的數字。上面的代碼有一個不同的問題:當隨機長度小於14位時,會導致StringIndexOutOfBoundsException。 – 2010-01-16 11:51:38
import java.util.Random;
private static void showRandomInteger(int aStart, int aEnd){
Random generator = new Random();
generator.setSeed(System.currentTimeMillis());
if (aStart > aEnd) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
//get the range, casting to long to avoid overflow problems
long range = (long)aEnd - (long)aStart + 1;
// compute a fraction of the range, 0 <= frac < range
long fraction = (long)(range * generator.nextDouble());
int randomNumber = (int)(fraction + aStart);
System.out.println("Generated : " + randomNumber);
}
你可以使用這種通用方法計算任意範圍內的隨機數。
Random r = new Random();
r.nextInt(bottomX-topX)+topX; //will give you the next random integer in range [bottomX,topX]
- 1. 生成幾何隨機數
- 2. 隨機數生成
- 3. 生成隨機數
- 4. 生成隨機數
- 5. 生成隨機數
- 6. 生成隨機數
- 7. 隨機數生成
- 8. 隨機數生成機制
- 9. 如何生成隨機UIColor?
- 10. 如何生成隨機圖?
- 11. 如何從短隨機數生成更長的隨機數?
- 12. 生成隨機數:計算隨機生成的x次數
- 13. 隨機數生成器幫助不生成隨機數 - C
- 14. 隨機()不生成隨機數
- 15. 從隨機長生成隨機數
- 16. 如何隨機生成數字?
- 17. 如何在django中生成隨機數
- 18. 如何生成隨機數列表?
- 19. 如何包括隨機數生成
- 20. 如何並行生成隨機數字?
- 21. 如何用arc4random生成隨機數字?
- 22. Matlab:如何生成僞隨機數
- 23. 如何創建生成隨機數
- 24. 如何在java中生成隨機數
- 25. AS3 - 如何生成隨機數字?
- 26. 如何用PHP生成隨機數?
- 27. Stata - 如何生成隨機整數
- 28. 如何生成大的隨機數C
- 29. 隨機數的生成過程如何?
- 30. 如何從陣列生成隨機數
我使用CLDC 1.0 MIDP 2.0 – Sarfraz 2010-01-16 16:22:29