随机码生成方法

1生成4位随机码,添加后缀.

    private static String random4Num() {
        int random = (int) ((Math.random() * 9 + 1) * 1000);
        return String.valueOf(random);
    }

2生成6位随机验证码

    private static String random6Num() {
        int random = (int) ((Math.random() * 9 + 1) * 100000);
        return String.valueOf(random);
    }

3生成随机数最优算法

    public static void main(String[] args) {

        int[] array = {0,1,2,3,4,5,6,7,8,9};
        Random random = new Random();

        for (int i = 10; i > 1; i--){
            int idx = random.nextInt(i);
            int temp = array[idx];
            array[idx] = array[i-1];
            array[i-1] = temp;
        }
        int result = 0;
        for (int j = 0; j < 4; j++){
          result = result*10 + array[j];
        }
        System.out.println(result);
    }

 

posted @ 2017-08-26 14:13  疯狂的蜗牛仔  阅读(800)  评论(0编辑  收藏  举报