随机数生成的简单原理

//取随机数
        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种子时,程序会默认用时间做种子。

posted @ 2015-12-17 09:02  D(a/e)mon  阅读(994)  评论(0编辑  收藏  举报