Java生成随机数的三种方式

package cn.zytao.taosir.random;

import java.util.Random;

public class RandomDemo {
    
    private static Integer byNewRandom() {
        Random r=new Random();
        return r.nextInt(100);
    }
    
    private static Integer byMathRandom() {
        int max=100,min=1;
        return (int)(Math.random()*(max-min)+min);
    }
    
    private static Integer byCurrentTimeMillis() {
        int max=100,min=1;
        long randomNum=System.currentTimeMillis();
        return (int)(randomNum%(max-min)+min);
    }
    
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //System.out.println(byNewRandom());
            //System.out.println(byMathRandom());
            System.out.println(byCurrentTimeMillis());
        }
    }
}

 

posted @ 2019-02-11 15:34  涛先森の日常  阅读(2985)  评论(0编辑  收藏  举报