码迷,mamicode.com
首页 > 编程语言 > 详细

Java中Random类

时间:2020-02-16 20:35:22      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:随机   stat   oid   public   int   exti   str   成员   static   

Random:产生随机数的类

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

成员方法:
public int nextInt();返回的是int范围的随机数
public int nextInt(int n):返回的是[0,n]范围的内随机数


public static void main(String[] args){
//创建对象
Random r = new Random();

for(int x = 0;x<10;x++){
//int num = r.nextInt();
int num = r.nextInt(100) + 1;
System.out.println(num);
}
}

Java中Random类

标签:随机   stat   oid   public   int   exti   str   成员   static   

原文地址:https://www.cnblogs.com/lszbk/p/12318438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!