Java生成6位随机数验证码

第一种方法:/**
 * //生成6位随机数,测试多次后有产生5位随机数的bug
 */
public static int randomCode() {
    StringBuilder str = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < 6; i++) {
        str.append(random.nextInt(10));
    }
    return Integer.parseInt(str.toString());
}
 
 

第二种:

/**
 * //生成6位随机数
 */
public static int randomCode() {
    return (int) ((Math.random() * 9 + 1) * 100000);
}

猜你喜欢

转载自blog.csdn.net/q_linchao/article/details/79474902
今日推荐