/**
* 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. 项目Beta冲刺(团队)——05.28(6/7)

    项目Beta冲刺(团队)--05.28(6/7) 格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺(团队) 团队名称:为了交项目干杯 作业目标:记录Beta敏捷冲刺第6 ...

  2. C语言中一个语句太长用什么换行?

     C语言中一个语句太长用什么换行? 5 C语言中一个语句太长用什么换行?比如我有一个printf语句很长很长,问了美观,我不想写在这一行了,要换到下一行,是不是在这行结尾的时候,要用个什么标识来表 ...

  3. Collections.synchronizedList使用

    1.SynchronizedList类具体代码: static class SynchronizedList<E> extends SynchronizedCollection<E& ...

  4. 数据库join解释 与视图

    数据库的视图是表运算的结果. 数据库的表是数据单元: join是运算符: 视图是运算结果. 数据库join解释 1.join:将两个表结构连接成一个视图 2.left.right.inner: 从基准 ...

  5. 本地部署Easy Mock

    最近在自己捣腾个vue的项目,苦于没有接口测试.网上搜寻一遍,基本上是使用mock.js模拟数据.研究mock.js 过程中,发现很多人提到了Easy Mock,发现它更加的方便.但是访问Eash M ...

  6. count to any

    A small computer game, puzzle, decryption

  7. CVE-2017-7494复现 Samba远程代码执行

    Samba是在Linux和Unix系统上实现Smb协议的一个免费软件,由服务器及客户端程序构成,Samba服务对应的TCP端口有139.445等.Smb一般作为文件共享服务器,专门提供Linux与Wi ...

  8. 为知笔记docker 版本运行

    最近为知提供了服务端的docker 运行,因为是全家桶,镜像偏大,但是使用还很不错,对于少于5人的可以免费使用 docker-compose 文件 version: "3" ser ...

  9. 洛谷P1107 [BJWC2008]雷涛的小猫 题解

    题面 以下是luogu给的标签 但字符串是什么鬼.... 玄学... 哦吼~ #include<cstdio> #include<iostream> using namespa ...

  10. IO多路复用之select poll epoll

    参考文档: http://blog.csdn.net/tennysonsky/article/details/45745887 select(),poll(),epoll()都是I/O多路复用的机制. ...