我是靠谱客的博主 细腻香菇,这篇文章主要介绍Android 生成随机数,现在分享给大家,希望可以做个参考。

 随机数字

            // 生成6位随机数字
            Logger.e((int) ((Math.random() * 9 + 1) * 100000) + "");
            // 生成5位随机数字
            Logger.e((int) ((Math.random() * 9 + 1) * 10000) + "");
            // 生成4位随机数字
            Logger.e((int) ((Math.random() * 9 + 1) * 1000) + "");
            // 生成3位随机数字
            Logger.e((int) ((Math.random() * 9 + 1) * 100) + "");
            // 生成2位随机数字
            Logger.e((int) ((Math.random() * 9 + 1) * 10) + "");
            // 生成1位随机数字
            Logger.e((int) ((Math.random() * 9 + 1)) + "");

6位随机字符(字母+数字)

    public String getRandomCode() {
        String randomcode = "";
        // 用字符数组的方式随机
        String model = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] m = model.toCharArray();
        for (int j = 0; j < 6; j++) {
            char c = m[(int) (Math.random() * 36)];
            // 保证六位随机数之间没有重复的
            if (randomcode.contains(String.valueOf(c))) {
                j--;
                continue;
            }
            randomcode = randomcode + c;
        }
        return randomcode;
    }

最后

以上就是细腻香菇最近收集整理的关于Android 生成随机数的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(128)

评论列表共有 0 条评论

立即
投稿
返回
顶部