栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

随机生成6位验证码具体方法(Random、Math.random()、ThreadLocalRandom)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

随机生成6位验证码具体方法(Random、Math.random()、ThreadLocalRandom)

文章目录

1. 需求2. 多种实现方式

1. Random 随机生成+ StringBuilder(不建议)2. Math.random()3. ThreadLocalRandom方法(推荐)4. String.format + ThreadLocalRandom

1. 需求
    有时候我们会有验证码的功能,自然就需要随机生成6位验证码的代码
2. 多种实现方式 1. Random 随机生成+ StringBuilder(不建议)
public class demo {
    public static void main(String[] args) {
    	// 模拟生成10次 6位随机数
        for (int i = 0; i < 10; i++) {
            System.out.println(random1());
        }
    }

    private static String random1() {
        StringBuilder stringBuilder = new StringBuilder();
			// 使用 Random随机生成数
        Random random = new Random();
        while (stringBuilder.length() < 6){
        	// 定义6位长度,每次随机生成一位数字,存在stringBuilder中
            stringBuilder.append(random.nextInt(10));
        }
        return stringBuilder.toString();
    }
}

2. Math.random()

Math.random()是jdk自带随机生成数

public class demo {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(random2());
        }
    }
	// 随机生成6位数字并转化为字符串
    public static String random2(){
        return String.valueOf((int)((Math.random() * 9 + 1) * 100000));
    }
 }
// 上述代码说明
Math.random() 是jdk自带随机生成数
0. 先看下面这段代码:
public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(Math.random());
        }
}

运行结果如下:
0.11855473622660206
0.5477513603228504
0.05680854872485608
0.9862235571729049
0.9425669037806845
0.01812945653873943
0.5264557778640957
0.31512329056746724
0.8615659469911732
0.38332415374172824

可以看出Math.random()是随机生成小数:
为了增加散列性,让生成的数字更加随机优化如下:
public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
        	// * 9
            System.out.println(Math.random()*9);
        }
}

运行结果如下:
6.636388224029258
5.996318003162425
4.81127425699719
4.434246696634742
3.0619944525025016
3.9558441357839893
4.9326953049048985
1.705501959550523
6.90840726813288
4.994018177873153

1. 那只要拿随机生成的数 * 100000 再取整 就可以获取 6位随机数了,那
2. 为什么还要 + 1 操作呢
3. 如果你随机生成的数目是 0.05680854872485608
4. 再*9 也大概是等于 0.49xxxxxxxxxx
5. 此时再*100000 也只剩 5位数。
6. 因此 在 Math.random()*9 + 1 操作是为了保证小数点右边一定不为0.
3. ThreadLocalRandom方法(推荐)
public class demo {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(random3());
        }
    }

    private static String random3() {
    	// jdk1.7出的随机生成数,关键还并发安全
    	// nextInt(int origin, int bound) 范围:[origin,bound)
    	// 包左 不包右
        return String.valueOf(ThreadLocalRandom.current().nextInt(100000, 1000000));
    }
}

4. String.format + ThreadLocalRandom

    有的时候又觉得上面的第三个方法ThreadLocalRandom的参数太麻烦了,也可以使用String.format补零的方式:

    public class demo {
        public static void main(String[] args) {
            for (int i = 0; i < 10; i++) {
                System.out.println(random4());
            }
        }
    
        private static String random4() {
            return String.format("%06d", ThreadLocalRandom.current().nextInt(1000000));
        }
    }
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709172.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号