共計 597 個字符,預計需要花費 2 分鐘才能閱讀完成。
- 使用 Math 類的 random() 方法生成 0 到 1 之間的隨機數:
double randomNum = Math.random();
- 使用 Random 類的 nextInt() 方法生成指定范圍內的隨機整數:
Random random = new Random();
int randomInt = random.nextInt(100); // 生成 0 到 99 的隨機整數
- 使用 ThreadLocalRandom 類的 nextInt() 方法生成指定范圍內的隨機整數:
int randomInt = ThreadLocalRandom.current().nextInt(0, 100); // 生成 0 到 99 的隨機整數
- 使用 SecureRandom 類的 nextInt() 方法生成安全的隨機整數:
SecureRandom random = new SecureRandom();
int randomInt = random.nextInt(100); // 生成 0 到 99 的安全隨機整數
- 使用 Apache Commons Math 庫中的 RandomDataGenerator 類生成指定范圍內的隨機數:
RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
int randomInt = randomDataGenerator.nextInt(0, 100); // 生成 0 到 99 的隨機整數
丸趣 TV 網 – 提供最優質的資源集合!
正文完