Random类的学习

Random:是一个可以获取随机数的类
 
 public Random():无参构造方法
 public Random(long seed) :指定long类型的数据进行构造随机数类对象 
 public int nextInt():获取随机数,它的范围是在int类型范围之内

 public int nextInt(int n):获取随机数,它的范围是在[0,n)之间

我们可以通过一段代码具体分析以上功能

public static void main(String[] args) {
		
		//创建Random类对象
		Random r = new Random() ;
		
		for(int x = 0 ; x < 10 ; x ++) {
			int n = r.nextInt(5) ;
			System.out.println(n);
		}//out:0
0
2
1
3
4
3
1
3
2
即输出0-5不包括5的随机数,输出10个。


猜你喜欢

转载自blog.csdn.net/luzhu1234/article/details/80176735