Random:产生随机数的类

来源:互联网 发布:论坛源码3.4 编辑:程序博客网 时间:2024/05/22 15:00
 1 /* 2  * Random:产生随机数的类 3  *  4  * 构造方法: 5  *         public Random():没有给种子,用的是默认种子,是当前时间的毫秒值 6  *        public Random(long seed):给出指定的种子 7  * 8  *        给定种子后,每次得到的随机数是相同的。 9  *10  * 成员方法:11  *         public int nextInt():返回的是int范围内的随机数12  *        public int nextInt(int n):返回的是[0,n)范围的内随机数13  */14 public class RandomDemo {15     public static void main(String[] args) {16         // 创建对象17         // Random r = new Random();18         Random r = new Random(1111);19 20         for (int x = 0; x < 10; x++) {21             // int num = r.nextInt();22             int num = r.nextInt(100) + 1;23             System.out.println(num);24         }25     }26 }

 

0 0
原创粉丝点击