Java获取固定长度随机数

 /**
     * 返回长度为【strLength】的随机数,在前面补0
     * @param strLength 长度
     * @return 随机数
     */
    public static String getFixLengthString(int strLength) {

        Random rm = new Random();

        // 获得随机数
        double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);

        // 将获得的获得随机数转化为字符串
        String fixLengthString = String.valueOf(pross);

        // 返回固定的长度的随机数
        return fixLengthString.substring(1, strLength + 1);
    }

转自 https://www.jianshu.com/p/a272f3da6559

posted @ 2019-10-10 16:06  jamess  阅读(2249)  评论(0编辑  收藏  举报