/**
* 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 随机密码的更多相关文章

  1. java Random.nextInt()方法

    转: java Random.nextInt()方法 lic int nextInt(int n) 该方法的作用是生成一个随机的int值,该值介于[0,n)的区间,也就是0到n之间的随机int值,包含 ...

  2. java Random类详解

    java Random类位于java.util包下,主要用来生成随机数,本文详解介绍了Random类的用法,希望能帮到大家 Random类 (java.util) Random类中实现的随机算法是伪随 ...

  3. Java Random介绍

    一.简介 Random类位于java.util包下,此类的实例用于生成伪随机数流.之所以称之为伪随机,是因为真正意义上的随机数(或者称为随机事件)在某次产生过程中是按照实验过程表现的分布概率随机产生的 ...

  4. JAVA Random 随机类

    nextInt 方法 得到一个随机整数, 可以指定范围 package object; import static net.util.Print.*; import java.util.Random; ...

  5. java获取随机密码

    import java.util.Random; public class tests { /** * * author LiuQiang * date 2013-10-14 下午01:13:54 * ...

  6. java Random类和Math.Rondom

      Java中存在着两种Random函数: 一.java.lang.Math.Random; 调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取 ...

  7. Java——Random类随机整数---18.10.11

    一.Random类的定义 1.Random类位于java.util包中,主要用于生成 伪随机数 2.random类将 种子数 作为随机算法的起源数字,计算生成伪随机数,其与生成的随机数字的区间无关 3 ...

  8. java random配置修改

    不知道 报啥错的时候 ,改这个 vim /usr/java/latest/jre/lib/security/java.security 原值: securerandom.source=file:/de ...

  9. 11. java random类

    一.random类使用 import java.util.Random; public class Demo{ public static void main(){ Random r = new Ra ...

随机推荐

  1. java 的任意进制间转换(很方便)

    import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = n ...

  2. 项目Beta冲刺--6/7

    项目Beta冲刺--6/7 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Beta冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及 ...

  3. jsbridge与通信模型

    三层通信模型: 应用层.解释层.会话层: 通信协议: 通信原语: 报文格式: 网络层: _evaluateJavascript 会话层: #define kQueueHasMessage   @&qu ...

  4. redux的知名ppt

    https://slides.com/jenyaterpil/redux-from-twitter-hype-to-production#/20

  5. LeetCode 1091. Shortest Path in Binary Matrix

    原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 题目: In an N by N square grid, ...

  6. 洛谷 P1120 小木棍 题解

    每日一题 day54 打卡 Analysis 一,管理员已经在题目中告诉你输入时去掉长度大于50的木棍. 二,想好搜索什么.很明显我们要枚举把哪些棍子拼接成原来的长棍,而原始长度(原来的长棍的长度)都 ...

  7. SQL盲注学习-布尔型

    本次实验还是使用sqli-labs环境.在开始SQL盲注之前首先学习一下盲注需要用到的基础语法. 1.left()函数:left(str,lenth)它返回具有指定长度的字符串的左边部分. left( ...

  8. [Python] Python忽略warning警告错误

    Python忽略warning警告错误   1)代码中警告 import warnings warnings.filterwarnings("ignore") 2)忽略命令行下警告 ...

  9. 一起学Makefile(六)

    命令的回显: 通常,make在执行命令之前都会把执行的命令进行输出,例如: 关闭命令回显有以下几种方式: 每个需要关闭回显的命令行之前加上”@”符号: 执行make时机上参数-s 或 –slient进 ...

  10. create-react-app不支持less的解决方式

    进入node_modules\react-scripts\config目录 修改webpack.config.dev.js跟webpack.config.prod.js中关于loader的配置即可,注 ...