java中生成随机数的方法

2014-11-24 10:21:39 · 作者: · 浏览: 0
[java]
package com.itheima.Math;

import java.util.Random;

public class RandomDemo {
public static void main(String[] args) {

Random r = new Random();

for (int x = 0; x < 10; x++) {
// int d = (int) (Math.random() * 10 + 1);//第一种:Math类中random();生成随机数
int d = r.nextInt(10)+1; //第二种:util包中的Random 类中的方法 。
sop(d);
}
}

/**
* @param d
*/
private static void sop(Object obj) {
System.out.println(obj);
}

}