java中Random类的详解(新手必备)

java中Random类的详解

Random:产生随机数的类

构造方法:

  • public Random():没有给种子,用的是默认种子,是当前时间的毫秒值
  • public Random(long seed):给出指定的种子
  • 给定种子后,每次得到的随机数是相同的。

成员方法:

  • public int nextInt():返回的是int范围内的随机数
  • public int nextInt(int n):返回的是[0,n)范围的内随机数
class test{
	public static void main(String[] args){
		//创建对象
		Random r = new Random();
		//测试public int nextInt();
		for(int i = 0 ; i <= 10 ; i++){
			int nun = r.nextInt();
		}
		//输出结果为:
				//635949163
				//34669087
				//-1846857952
				//851495967
				//654129567
				//-227395044
				//-1608876680
				//1726408963
				//-1517057225
				//1526309759
				//-1185048128


		//测试public int nextInt(int n);
		for(int i = 0 ; i <= 10 ; i++){
			int nun = r.nextInt(100);
			System.out.println(num);
			//输出结果为:55  13  38  65  20  47  54  55  2  12  71 
		}
	}
}
import java.util.Random;
class test{
	public static void main(String[] args){
		//创建对象
		Random r = new Random(111);
		for(int i = 0 ; i <= 5 ; i++){
			int num = r.nextInt(100);
			System.out.print(num+" ");
			//输出结果:无论运行多少次输出结果都是93 70 57 97 9 20 ,不会改变	
		}	
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值