0
0

More than 3 years have passed since last update.

【Java】乱数生成の方法(Random)

Last updated at Posted at 2019-10-29

Javaで簡単に乱数を生成するプログラム例を紹介します。

整数型(0~の数字)

int r = new java.util.Random().nextInt(※発生させる乱数の上限値);

ソースのはじめにimport java.util.*をしているなら以下のように書くことも可能です。

int r = new Random().nextInt(※発生させる乱数の上限値);

参考:java.util.Random#nextInt(int) の仕様

小数型(0~1の乱数を生成するパターン)

double r = Math.random();
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0