java Random 随机密码
/**
* Created by xc on 2019/11/23
* 生成随机密码:6位数字
*/
public class Test7_4 { public static void main(String[] args) {
System.out.println(randomPassword());//382630
} public static String randomPassword() {
char[] chars = new char[6];
Random rnd = new Random();
for (int i = 0; i < 6; i++) {
chars[i] = (char) ('0' + rnd.nextInt(10));
}
return new String(chars);
} }
/**
* Created by xc on 2019/11/23
* 生成随机密码:简单8位
* 8位密码,字符可能由大写字母、小写字母、数字和特殊符号组成
*/
public class Test7_5 { private static final String SPECIAL_CHARS = "!@#$%^&*_=+-/"; public static void main(String[] args) {
System.out.println(randomPassword());//ejgY^14*
} private static char nextChar(Random rnd) {
switch (rnd.nextInt(4)) {
case 0:
return (char) ('a' + rnd.nextInt(26));
case 1:
return (char) ('A' + rnd.nextInt(26));
case 2:
return (char) ('0' + rnd.nextInt(10));
default:
return SPECIAL_CHARS.charAt(rnd.nextInt(SPECIAL_CHARS.length()));
}
} public static String randomPassword() {
char[] chars = new char[8];
Random rnd = new Random();
for (int i = 0; i < 8; i++) {
chars[i] = nextChar(rnd);
}
return new String(chars);
} }
/**
* Created by xc on 2019/11/23
* 生成随机密码:复杂8位
*/
public class Test7_6 { private static final String SPECIAL_CHARS = "!@#$%^&*_=+-/"; public static void main(String[] args) {
System.out.println(randomPassword());//Q*82-/zQ
} private static int nextIndex(char[] chars, Random rnd) {
int index = rnd.nextInt(chars.length);
while (chars[index] != 0) {
index = rnd.nextInt(chars.length);
}
return index;
} private static char nextSpecialChar(Random rnd) {
return SPECIAL_CHARS.charAt(rnd.nextInt(SPECIAL_CHARS.length()));
} private static char nextUpperlLetter(Random rnd) {
return (char) ('A' + rnd.nextInt(26));
} private static char nextLowerLetter(Random rnd) {
return (char) ('a' + rnd.nextInt(26));
} private static char nextNumLetter(Random rnd) {
return (char) ('0' + rnd.nextInt(10));
} public static String randomPassword() {
char[] chars = new char[8];
Random rnd = new Random();
chars[nextIndex(chars, rnd)] = nextSpecialChar(rnd);
chars[nextIndex(chars, rnd)] = nextUpperlLetter(rnd);
chars[nextIndex(chars, rnd)] = nextLowerLetter(rnd);
chars[nextIndex(chars, rnd)] = nextNumLetter(rnd);
for (int i = 0; i < 8; i++) {
if (chars[i] == 0) {
chars[i] = nextChar(rnd);
}
}
return new String(chars);
} private static char nextChar(Random rnd) {
switch (rnd.nextInt(4)) {
case 0:
return (char) ('a' + rnd.nextInt(26));
case 1:
return (char) ('A' + rnd.nextInt(26));
case 2:
return (char) ('0' + rnd.nextInt(10));
default:
return SPECIAL_CHARS.charAt(rnd.nextInt(SPECIAL_CHARS.length()));
}
} }
java Random 随机密码的更多相关文章
- java Random.nextInt()方法
转: java Random.nextInt()方法 lic int nextInt(int n) 该方法的作用是生成一个随机的int值,该值介于[0,n)的区间,也就是0到n之间的随机int值,包含 ...
- java Random类详解
java Random类位于java.util包下,主要用来生成随机数,本文详解介绍了Random类的用法,希望能帮到大家 Random类 (java.util) Random类中实现的随机算法是伪随 ...
- Java Random介绍
一.简介 Random类位于java.util包下,此类的实例用于生成伪随机数流.之所以称之为伪随机,是因为真正意义上的随机数(或者称为随机事件)在某次产生过程中是按照实验过程表现的分布概率随机产生的 ...
- JAVA Random 随机类
nextInt 方法 得到一个随机整数, 可以指定范围 package object; import static net.util.Print.*; import java.util.Random; ...
- java获取随机密码
import java.util.Random; public class tests { /** * * author LiuQiang * date 2013-10-14 下午01:13:54 * ...
- java Random类和Math.Rondom
Java中存在着两种Random函数: 一.java.lang.Math.Random; 调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取 ...
- Java——Random类随机整数---18.10.11
一.Random类的定义 1.Random类位于java.util包中,主要用于生成 伪随机数 2.random类将 种子数 作为随机算法的起源数字,计算生成伪随机数,其与生成的随机数字的区间无关 3 ...
- java random配置修改
不知道 报啥错的时候 ,改这个 vim /usr/java/latest/jre/lib/security/java.security 原值: securerandom.source=file:/de ...
- 11. java random类
一.random类使用 import java.util.Random; public class Demo{ public static void main(){ Random r = new Ra ...
随机推荐
- live555流媒体框架介绍
LIVE555 Streaming Media This code forms a set of C++ libraries for multimedia streaming, using open ...
- Gradle 学习资料
Gradle 学习资料 网址 Gradle 使用指南 http://wiki.jikexueyuan.com/project/gradle/ 寄Android开发Gradle你需要知道的知识 http ...
- PHP:ThinkCMFX任意文件包含漏洞
前言:最近爆出来的漏洞,ThinkCmfX版本应该是通杀的,基于3.X Thinkphp开发的 代码下载地址:https://gitee.com/thinkcmf/ThinkCMFX/releases ...
- 22-1 web传输视频 Opencv+usb摄像头 树莓派+Flask实现视频流媒体WEB服务器
第一篇 讲解原理 https://blog.miguelgrinberg.com/post/video-streaming-with-flask 第二篇 加入多线程可以直接用 https://gith ...
- ent 基本使用四 图遍历查询
接上文,我们已经创建了基本的关系以及表实体,以下是通过图方式的查询 参考关系图 代码处理 创建图数据 func CreateGraph(ctx context.Context, client *ent ...
- Cronicle 基于nodejs 的分布式任务调度工具
Cronicle 是一款不多的分布式任务调度工具,基于nodejs 开发,同时包含看web UI,从官方介绍看到的UI还是 挺不错的,而且功能很丰富,值得试用下 参考资料 http://cronicl ...
- 7.学习springmvc的异常处理
一.页面的异常处理流程 二.建立项目springmvc04_exception 1.编写index.jsp页面: <%@ page contentType="text/html;cha ...
- stack的简单用法总结
stack中常见方法 top():返回一个栈顶元素的引用,类型为 T&.如果栈为空,返回值未定义. push(const T& obj):可以将对象副本压入栈顶.这是通过调用底层容器的 ...
- UDF——计算壁面边界合力矩(旋转机械)
- JS字符串转换为JSON的四种方法
转自:https://www.cnblogs.com/hgmyz/p/7451461.html 1.jQuery插件支持的转换方式: 示例: $.parseJSON( jsonstr ); //jQ ...