史上最全的java随机数生成算法分享
时间:2023-10-17 15:22:33
String password = RandomUtil.generateString(10);
源码如下:
package com.javaniu.core.util;
import java.util.Random;
public class RandomUtil {
public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String NUMBERCHAR = "0123456789";
/**
* 返回一个定长的随机字符串(只包含大小写字母、数字)
*
* @param length
* 随机字符串长度
* @return 随机字符串
*/
public static String generateString(int length) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
}
return sb.toString();
}
/**
* 返回一个定长的随机纯字母字符串(只包含大小写字母)
*
* @param length
* 随机字符串长度
* @return 随机字符串
*/
public static String generateMixString(int length) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));
}
return sb.toString();
}
/**
* 返回一个定长的随机纯大写字母字符串(只包含大小写字母)
*
* @param length
* 随机字符串长度
* @return 随机字符串
*/
public static String generateLowerString(int length) {
return generateMixString(length).toLowerCase();
}
/**
* 返回一个定长的随机纯小写字母字符串(只包含大小写字母)
*
* @param length
* 随机字符串长度
* @return 随机字符串
*/
public static String generateUpperString(int length) {
return generateMixString(length).toUpperCase();
}
/**
* 生成一个定长的纯0字符串
*
* @param length
* 字符串长度
* @return 纯0字符串
*/
public static String generateZeroString(int length) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
sb.append('0');
}
return sb.toString();
}
/**
* 根据数字生成一个定长的字符串,长度不够前面补0
*
* @param num
* 数字
* @param fixdlenth
* 字符串长度
* @return 定长的字符串
*/
public static String toFixdLengthString(long num, int fixdlenth) {
StringBuffer sb = new StringBuffer();
String strNum = String.valueOf(num);
if (fixdlenth - strNum.length() >= 0) {
sb.append(generateZeroString(fixdlenth - strNum.length()));
} else {
throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
+ "的字符串发生异常!");
}
sb.append(strNum);
return sb.toString();
}
/**
* 每次生成的len位数都不相同
*
* @param param
* @return 定长的数字
*/
public static int getNotSimple(int[] param, int len) {
Random rand = new Random();
for (int i = param.length; i > 1; i--) {
int index = rand.nextInt(i);
int tmp = param[index];
param[index] = param[i - 1];
param[i - 1] = tmp;
}
int result = 0;
for (int i = 0; i < len; i++) {
result = result * 10 + param[i];
}
return result;
}
public static void main(String[] args) {
System.out.println("返回一个定长的随机字符串(只包含大小写字母、数字):" + generateString(10));
System.out
.println("返回一个定长的随机纯字母字符串(只包含大小写字母):" + generateMixString(10));
System.out.println("返回一个定长的随机纯大写字母字符串(只包含大小写字母):"
+ generateLowerString(10));
System.out.println("返回一个定长的随机纯小写字母字符串(只包含大小写字母):"
+ generateUpperString(10));
System.out.println("生成一个定长的纯0字符串:" + generateZeroString(10));
System.out.println("根据数字生成一个定长的字符串,长度不够前面补0:"
+ toFixdLengthString(123, 10));
int[] in = { 1, 2, 3, 4, 5, 6, 7 };
System.out.println("每次生成的len位数都不相同:" + getNotSimple(in, 3));
}
}
有图有真相:
java随机字符补充版
今天在zuidaimai看到一个java随机字符生成demo,正好要用,但发现不完整,重新整理一下,分享给有需要的朋友
public static void main(String[] args) {
//String s = RandomNum.getRandomNumStr(5);
//System.out.println(s);
System.out.println("生成5个含有5个字符的字符串:");
RandomNum.SuiJiZiFuChuan(5,5);
System.out.println("生成3个含有6个字符的字符串:");
RandomNum.SuiJiZiFuChuan(6,3);
System.out.println("生成任意1到20个含有任意1到10个字符的字符串:");
RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),(int)(10*Math.random()));
System.out.println("随机性生成字符:");
int i=0;
while(i<(int)(10*Math.random())){
RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),1);
i++;
}
}
public static void SuiJiZiFuChuan(int x,int y){
for(int j=0;j<y;j++){
for(int i=0;i<x;i++){
int a=(int)(100*Math.random()+100*Math.random());
while(true){
if(a>96&a<123)
break;
else
a=(int)(100*Math.random()+100*Math.random());
}
System.out.print((char)a);
}
System.out.println();
}
}
执行结果:
来源: http://www.zuidaima.com/share/1585762703215616.htm
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot-jta-atomikos多数据源事务管理实现
![](https://img.aspxhome.com/file/2023/8/125558_0s.png)
OpenGL实现Bezier曲线的方法示例
![](https://img.aspxhome.com/file/2023/6/112286_0s.jpg)
C#实战之备忘录的制作详解
![](https://img.aspxhome.com/file/2023/8/116638_0s.jpg)
Adapter模式实战之重构鸿洋集团的Android圆形菜单建行
![](https://img.aspxhome.com/file/2023/2/115992_0s.gif)
Java抽象类与接口区别详解
![](https://img.aspxhome.com/file/2023/5/76265_0s.png)
SpringBoot实现RAS+AES自动接口解密
![](https://img.aspxhome.com/file/2023/5/119785_0s.jpg)
SpringCloud Eureka服务治理之服务注册服务发现
![](https://img.aspxhome.com/file/2023/0/79950_0s.png)
Java基础之从HelloWorld到面向对象
![](https://img.aspxhome.com/file/2023/4/125594_0s.png)
Android 调用百度地图API示例
![](https://img.aspxhome.com/file/2023/2/139522_0s.png)
使用MAT进行JVM内存分析实例
![](https://img.aspxhome.com/file/2023/8/103988_0s.png)
8种android 对话框(Dialog)使用方法详解
c# 实现控件(ocx)中的事件详解
![](https://img.aspxhome.com/file/2023/5/88305_0s.jpg)
Java对xls文件进行读写操作示例代码
C#使用SQL Dataset数据集代码实例
Android实现图片转高斯模糊以及高斯模糊布局
使用java.nio.file 库优雅的操作文件详解
![](https://img.aspxhome.com/file/2023/7/132077_0s.png)
Mybatis 级联删除的实现
java实现简单计算器功能
![](https://img.aspxhome.com/file/2023/2/74772_0s.jpg)
消息队列-kafka消费异常问题
![](https://img.aspxhome.com/file/2023/4/118914_0s.jpg)
Android复选框CheckBox与开关按钮Switch及单选按钮RadioButton使用示例详解
![](https://img.aspxhome.com/file/2023/4/109184_0s.png)