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 ...
随机推荐
- pandas的行列显示不全的解决方法
pd.set_option('display.max_rows', 100) # 显示的最大行数(避免只显示部分行数据) pd.set_option('display.max_columns', 10 ...
- discuz添加标签聚合页
discuz使用视频教程前段时间把discuz原本的标签聚合页修改了一下,原本的标签聚合页只显示100条最新的标签,把它修改成了所有标签都显示,并加上分页. 修改方法: 1.修改/source/mod ...
- autoRoll_UpDown()|上下滚动函数|无缝|自动(自带demo)
autoRoll_UpDown函数 function autoRoll_UpDown($domObj,speed){ //$domObj 外围容器的jQuery式元素对象 speed 滚动速度,单位毫 ...
- 硬币游戏1——打表&&记忆化搜索
题目 Alice和Bo在玩这样一个游戏,给定 $k$ 个数字 $a_1, a_2,..,a_k$.一开始有 $x$ 枚硬币,Alice和Bob轮流取硬币.每次所取的硬币的枚数一定要在 $k$ 个数当中 ...
- hihocoder#1046: K个串
[传送门] 这种区间内相同数字只能被统计一次/只有区间内数字都不相同才对答案有贡献的题都可以用扫描线扫右端点,表示当前区间右端点为$r$.然后当前线段树/树状数组维护区间左端点为$[1,r)$时对应的 ...
- memset使用技巧
memset可以对高位数组进行初始化,非常方便.需要注意的是memset的头文件是string.h和memory.h . 下面来谈memset的4个使用技巧: (注:一下dp高维数组都是全局变量,局部 ...
- jstl的if标签和forEach标签的解析
今天上午学习了jstl的if标签和forEach标签(其它标签用的很少,所以没讲,只讲了这两个标签),然后通过代码练习了一下,现在总结. 首先导入包,从Apache的网站下载JSTL的JAR包.进入 ...
- 第09组 Alpha冲刺(2/6)
队名:观光队 组长博客 作业博客 组员实践情况 王耀鑫 过去两天完成了哪些任务 文字/口头描述 完成服务器连接数据库部分代码 展示GitHub当日代码/文档签入记录 接下来的计划 服务器网络请求. 还 ...
- vue中使用时间插件、vue使用laydate
<input id="time1" readonly="readonly" placeholder="这里选择时间" v-model= ...
- 方法型混淆js代码
const fs = require('fs'); const acorn = require('acorn'); const walk = require("acorn-walk" ...