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

Java随机数

时间:2017-09-07 00:05:05      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:boolean   oat   log   ble   oid   next   使用   new   double   

使用Random类产生随机数

 1   @Test
 2     public void test1(){
 3         Random rd=new Random();
 4         System.out.println(rd.nextInt());      //2110276222
 5         System.out.println(rd.nextInt(10));    //5
 6         System.out.println(rd.nextFloat());    //0.92339724
 7         System.out.println(rd.nextDouble());   //0.12333168823610974
 8         System.out.println(rd.nextLong());     //-993158337391878589
 9         System.out.println(rd.nextBoolean());  //true
10     }

产生某个范围的随机数,比如[20,50]

 1   @Test
 2     public void test2(){
 3         Random rd=new Random();
 4         int max=50;
 5         int min=20;
 6         for (int i = 0; i < 20; i++) {
 7             int j=rd.nextInt(max-min+1)+min;
 8             System.out.println(j);
 9         }
10     }

 

Java随机数

标签:boolean   oat   log   ble   oid   next   使用   new   double   

原文地址:http://www.cnblogs.com/xnxbk/p/7487592.html

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