首页 > 其他 > 详细

随机数生成的简单原理

时间:2015-12-17 09:20:01      阅读:277      评论:0      收藏:0      [点我收藏+]
//取随机数
        System.out.println("随机数 =" + Math.random());
        
        System.out.println("随机数 =" + Math.random());
        
        System.out.println("随机数 =" + Math.random());
        
        System.out.println("随机数 =" + Math.random());
        
        
        Random r = new Random();//用时间做种子
        
        //r = new Random(1);//随机数种子
        
        System.out.println("Random随机数 = " + r.nextInt(1000));
        
        System.out.println("Random随机数 = " + r.nextInt(10));
        
        System.out.println("Random随机数 = " + r.nextInt(10));
        
        System.out.println("Random随机数 = " + r.nextInt(100));
        

输出为:

随机数 =0.7119181740904947
随机数 =0.3327430688570313
随机数 =0.3640785757582764
随机数 =0.006136746142825333
Random随机数 = 638
Random随机数 = 6
Random随机数 = 1
Random随机数 = 8

注:在不给随机数赋值 Random种子时,程序会默认用时间做种子。

随机数生成的简单原理

原文:http://www.cnblogs.com/fangchongyan/p/5052970.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!