/**
* 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. Tessy — 嵌入式软件单元测试/ 集成测试工具

    Tessy 源自戴姆勒- 奔驰公司的软件技术实验室,由德国Hitex 公司负责全球销售及技术支持服务,是一款专门针对嵌入式软件进行单元/ 集成测试的工具.它可以对C/C++ 代码进行单元.集成测试,可 ...

  2. 如何测试Web服务.3

    -->全文字数:2254,需要占用你几分钟的阅读时间 ,您也可以收藏后,时间充足时再阅读- ->第一节讲了<Web服务基础介绍>,第二节讲了<Web服务测试工具> ...

  3. python-uiautomator2

    简单介绍 python-uiautomator2是一个自动化测试开源工具,仅支持Android平台的原生应用测试. 支持平台及语言 python-uiautomator2封装了谷歌自带的uiautom ...

  4. python案例-判断素数

    from math import sqrt def is_prime(num): for rea in range(2,int(sqrt(num)+1)): if num%rea==0: return ...

  5. 20180606模拟赛T1——猫鼠游戏

    题目描述: 猫和老鼠在10*10的方格中运动,例如: *...*..... ......*... ...*...*.. .......... ...*.C.... *.....*... ...*... ...

  6. 装饰器vue-property-decorator

    接触到了新的vue项目,使用vue+ts+vue-property-decotator来进行项目的简化,一时间语法没有看懂,所以花时间学习这个装饰器的包. 1.装饰器 @Component(optio ...

  7. js沉思录一:js的核心概念

    js的核心概念: 原型.对象(Object).函数(Function); 原型:路由路径上非叶子结点的对象: 对象:无序属性(包括函数)的集合: 函数:函数上下文的集合: 原型: 原型的创建.指定.修 ...

  8. IDEA+Maven+Mybatis 巨坑:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.UserMapper.findAll

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.User ...

  9. 我的.NET之路

    有时感觉知识比较零散,做个总结形成自己的知识体系,方便查阅[持续更新...] C#语法特性 .Net FrameWork发展史 C# 语言版本发展史 1.NET体系结构 [C#与.NET的关系.公共语 ...

  10. s3-sftp-proxy goreleaser rpm &&deb 包制作

    上次写过简单的s3-sftp-proxy基于容器构建以及使用goreleaser构建跨平台二进制文件的,下边演示下关于 rpm&&deb 包的制作,我们只需要简单的配置就可以生成方便安 ...