史上最全的java隨機(jī)數(shù)生成算法分享
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";
/**
* 返回一個(gè)定長(zhǎng)的隨機(jī)字符串(只包含大小寫字母、數(shù)字)
*
* @param length
* 隨機(jī)字符串長(zhǎng)度
* @return 隨機(jī)字符串
*/
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();
}
/**
* 返回一個(gè)定長(zhǎng)的隨機(jī)純字母字符串(只包含大小寫字母)
*
* @param length
* 隨機(jī)字符串長(zhǎng)度
* @return 隨機(jī)字符串
*/
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();
}
/**
* 返回一個(gè)定長(zhǎng)的隨機(jī)純大寫字母字符串(只包含大小寫字母)
*
* @param length
* 隨機(jī)字符串長(zhǎng)度
* @return 隨機(jī)字符串
*/
public static String generateLowerString(int length) {
return generateMixString(length).toLowerCase();
}
/**
* 返回一個(gè)定長(zhǎng)的隨機(jī)純小寫字母字符串(只包含大小寫字母)
*
* @param length
* 隨機(jī)字符串長(zhǎng)度
* @return 隨機(jī)字符串
*/
public static String generateUpperString(int length) {
return generateMixString(length).toUpperCase();
}
/**
* 生成一個(gè)定長(zhǎng)的純0字符串
*
* @param length
* 字符串長(zhǎng)度
* @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();
}
/**
* 根據(jù)數(shù)字生成一個(gè)定長(zhǎng)的字符串,長(zhǎng)度不夠前面補(bǔ)0
*
* @param num
* 數(shù)字
* @param fixdlenth
* 字符串長(zhǎng)度
* @return 定長(zhǎng)的字符串
*/
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("將數(shù)字" + num + "轉(zhuǎn)化為長(zhǎng)度為" + fixdlenth
+ "的字符串發(fā)生異常!");
}
sb.append(strNum);
return sb.toString();
}
/**
* 每次生成的len位數(shù)都不相同
*
* @param param
* @return 定長(zhǎng)的數(shù)字
*/
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("返回一個(gè)定長(zhǎng)的隨機(jī)字符串(只包含大小寫字母、數(shù)字):" + generateString(10));
System.out
.println("返回一個(gè)定長(zhǎng)的隨機(jī)純字母字符串(只包含大小寫字母):" + generateMixString(10));
System.out.println("返回一個(gè)定長(zhǎng)的隨機(jī)純大寫字母字符串(只包含大小寫字母):"
+ generateLowerString(10));
System.out.println("返回一個(gè)定長(zhǎng)的隨機(jī)純小寫字母字符串(只包含大小寫字母):"
+ generateUpperString(10));
System.out.println("生成一個(gè)定長(zhǎng)的純0字符串:" + generateZeroString(10));
System.out.println("根據(jù)數(shù)字生成一個(gè)定長(zhǎng)的字符串,長(zhǎng)度不夠前面補(bǔ)0:"
+ toFixdLengthString(123, 10));
int[] in = { 1, 2, 3, 4, 5, 6, 7 };
System.out.println("每次生成的len位數(shù)都不相同:" + getNotSimple(in, 3));
}
}
有圖有真相:
java隨機(jī)字符補(bǔ)充版
今天在zuidaimai看到一個(gè)java隨機(jī)字符生成demo,正好要用,但發(fā)現(xiàn)不完整,重新整理一下,分享給有需要的朋友
public static void main(String[] args) {
//String s = RandomNum.getRandomNumStr(5);
//System.out.println(s);
System.out.println("生成5個(gè)含有5個(gè)字符的字符串:");
RandomNum.SuiJiZiFuChuan(5,5);
System.out.println("生成3個(gè)含有6個(gè)字符的字符串:");
RandomNum.SuiJiZiFuChuan(6,3);
System.out.println("生成任意1到20個(gè)含有任意1到10個(gè)字符的字符串:");
RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),(int)(10*Math.random()));
System.out.println("隨機(jī)性生成字符:");
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();
}
}
執(zhí)行結(jié)果:
來源: http://www.zuidaima.com/share/1585762703215616.htm
- 如何用java生成指定范圍的隨機(jī)數(shù)
- Java中生成隨機(jī)數(shù)的實(shí)現(xiàn)方法總結(jié)
- Java生成隨機(jī)數(shù)的2種示例方法代碼
- java生成指定范圍隨機(jī)數(shù)的多種代碼
- Java實(shí)現(xiàn)生成n個(gè)不重復(fù)的隨機(jī)數(shù)
- Java編程實(shí)現(xiàn)生成給定范圍內(nèi)不重復(fù)隨機(jī)數(shù)的方法小結(jié)
- Java編程中隨機(jī)數(shù)的生成方式總結(jié)
- java生成隨機(jī)數(shù)(字符串)示例分享
- Java中隨機(jī)數(shù)生成常見的幾種方式及適用場(chǎng)景
相關(guān)文章
redisson.tryLock()參數(shù)的使用及理解
這篇文章主要介紹了redisson.tryLock()參數(shù)的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04Hibernate中l(wèi)oad方法與get方法的區(qū)別
Hibernate中有兩個(gè)極為相似的方法get()與load(),他們都可以通過指定的實(shí)體類與ID從數(shù)據(jù)庫(kù)中讀取數(shù)據(jù),并返回對(duì)應(yīng)的實(shí)例,但Hibernate不會(huì)搞兩個(gè)完全一樣的方法的2016-01-01Java8中Lambda表達(dá)式使用和Stream API詳解
這篇文章主要給大家介紹了關(guān)于Java8中Lambda表達(dá)式使用和Stream API的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Springboot中Instant時(shí)間傳參及序列化詳解
這篇文章主要介紹了Springboot中Instant時(shí)間傳參及序列化詳解,Instant是Java8引入的一個(gè)精度極高的時(shí)間類型,可以精確到納秒,但實(shí)際使用的時(shí)候不需要這么高的精確度,通常到毫秒就可以了,需要的朋友可以參考下2023-11-11關(guān)于設(shè)置Mybatis打印調(diào)試sql的兩種方式
這篇文章主要介紹了關(guān)于設(shè)置Mybatis打印調(diào)試sql的兩種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Spring?boot?啟動(dòng)流程及外部化配置方法
平時(shí)我們開發(fā)Spring boot 項(xiàng)目的時(shí)候,一個(gè)SpringBootApplication注解加一個(gè)main方法就可以啟動(dòng)服務(wù)器運(yùn)行起來,那它到底是怎么運(yùn)行起來的呢?這篇文章主要介紹了Spring?boot?啟動(dòng)流程及外部化配置,需要的朋友可以參考下2022-12-12SpringBoot獲取配置文件內(nèi)容的幾種方式總結(jié)
大家都知道SpringBoot獲取配置文件的方法有很多,下面這篇文章主要給大家介紹了關(guān)于SpringBoot獲取配置文件內(nèi)容的幾種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02