乱数を発生させるにはRandomクラスを利用する。(Mathクラスもあるが・・・)
--------------------------------
import java.util.Random;
/*
0~9までの乱数を10回標準出力へ
*/
public static void main(String[] args){
Random ran = new Random();
for(int i = 0; i < 10; i++){
System.out.println(ran.nextInt(10));
}
}
//※nextInt(n)は0~(n-1)の範囲の乱数が生成される
--------------------------------
Randomのメソッド
- nextBoolean() boolean型 trueまたはfalse
- nextInt() int型 -2147483648~+2147483647
- nextLong() long型 -9223372036854775808~+9223372036854775807
- nextDouble() double型 0.0以上1.0未満
- nextFloat() float型 0.0以上1.0未満
最終更新:2009年05月30日 18:56