博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
指定位数随机码生成
阅读量:6959 次
发布时间:2019-06-27

本文共 2118 字,大约阅读时间需要 7 分钟。

hot3.png

/** * 生成验证码 * * @param num 位数 * @return 纯数字验证码 */public static String generateAuthCode(int num) {    String chars = "0123456789";    char[] rands = new char[num];    for (int i = 0; i < num; i++) {        int rand = (int) (Math.random() * 10);        rands[i] = chars.charAt(rand);    }    return new String(rands);}
/** * 指定位数的随机数 * @param length * @return */public static String getRandomNumber(int length) {    char[] chr = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; Random random = new Random(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < length; i++) { buffer.append(chr[random.nextInt(chr.length)]); } return buffer.toString();}
/** * JAVA获得0-9,A-Z范围的随机数 * * @param length 随机数长度 * @return String */public static String getRandomChar0(int length) {    char[] chr = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',             'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; Random random = new Random(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < length; i++) { buffer.append(chr[random.nextInt(chr.length)]); } return buffer.toString();}/** * JAVA获得0-9,a-z,A-Z范围的随机数 * * @param length 随机数长度 * @return String */public static String getRandomChar1(int length) { char[] chr = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',             'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',             'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; Random random = new Random(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < length; i++) { buffer.append(chr[random.nextInt( chr.length )]); } return buffer.toString();}public static void main(String[] args) { System.out.println(getRandomChar1(6));}

 

其中,random.nexInt(int n)用于获取0到n的随机整数,不包括n

转载于:https://my.oschina.net/kevin2kelly/blog/886499

你可能感兴趣的文章
基于.NET平台常用的框架整理
查看>>
springmvc(3)拦截器HandlerInterceptor源码的简单解析
查看>>
初学Python(六)——输入输出
查看>>
用布局方式作出模仿微信选项卡 1
查看>>
Java虚拟机的启动与程序的运行
查看>>
几个有用的java 7特性
查看>>
jS数组
查看>>
php函数xml转化数组
查看>>
五分钟读懂UML类图
查看>>
sql case 函数与详细说明
查看>>
旋转矩阵(模拟)
查看>>
点头(1163)
查看>>
js传输图片路径
查看>>
HDU 6029 Graph Theory【水题】
查看>>
HDU 4053 or ZOJ 3541 The Last Puzzle【区间dp】【经典题】
查看>>
1163: [Baltic2008]Mafia
查看>>
物联网系统框架介绍
查看>>
centos6 安装 ansible_ui
查看>>
搭建区块链浏览器——基于hyperledger fabric 1.0,MySQL容器
查看>>
[BZOJ4372]烁烁的游戏
查看>>