Java 中Random类随机数生成器

*随机数生成器Random类
 * 
 * public Random()产生一个随机数生成器
 * public Random(long seed):创建随机数生成器的同时,给固定时间值
 *                             它产生的随机数永远是固定的
 * 成员方法:
 *         public int nextInt():取值范围是int类型的范围
 *         public int nextInt(n):可以获取0~n之间的随机数,取不到n
 *    获取随机数:两种
 *        Math里面random方法
 *                    Math.random();
 *        Random类:nextInt(int n)
 *            用法:
 *                Random()空参构造+nextInt(int n):获取随机数

public class RandomDemo {
	public static void main(String[] args) {
		//创建随机数生成对象
//		Random r=new Random();
		Random r=new Random(1111);
		//产生10个随机数
		for(int x=0;x<10;x++) {
//			 public Random()
//			int num=r.nextInt();
//			System.out.println(num);
			int num=r.nextInt(20)+1;
			System.out.println(num);
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值