使用RandomStringUtils,可以選擇生成随機字元串,可以是全字母、全數字或自定義生成字元等等...
其最基礎的方法如下:
public static String random(int count, int start, int end, boolean letters,
boolean numbers, char[] chars, Random random) {}
參數解讀:
count:需要生成的随機串位數
letters:隻要字母?
numbers:隻要數字?
chars:自定義生成字元數組,如果為null,則為所有字元
start、end:選擇字元開始生成的位置-----如果chars為null,start就是ASCII你想開始生成字元的位置,end同理;chars不為空的話,就是你自定義字元數組開始生成字元的位置
random:随機源
API:
序号 | 方法 | 說明 |
---|---|---|
1 | random(int count) | 在所有字元中随機生成6位 |
2 | randomAscii(int count) | 在ASCII表中的列印字元中,即ASCII表32-127中随機生成6位 |
3 | randomAlphabetic(int count) | 生成隻有字母的随機字元串,但是此方法效率不高,不建議這樣使用 |
4 | randomAlphanumeric(int count) | 生成隻有字母和數字的随機字元串,同樣不建議這樣使用 |
5 | randomNumeric(int count) | 生成隻有數字的随機字元串,同樣不建議這樣使用 |
6 | random(int count, boolean letters, boolean numbers) | 可以指定是否要限定隻生成字母或數字,上述三個方法都是調用此方法 |
7 | random(int count, int start, int end, boolean letters, boolean numbers) | 可以指定字元集開始的位置,即不用搜尋所有全部字元,同時可指定是否指定是否隻生成字母或數字 |
8 | random(int count, int start, int end, boolean letters, boolean numbers, char[] chars) | 用指定的字元集生成字元串 |
9 | random(int count, String chars) | 使用指定字元串的字元生成新的随機字元串 |
10 | String random(int count, char[] chars) | 用指定的字元集生成字元串,隻是不能指定letters和numbers,其實還是調用了8 |
e.g.
按照源碼順序使用,其源碼其實都是在調用基礎方法
@Test
public void RandomStringUtilsTest(){
System.out.println("RandomStringUtils.random1-->" + RandomStringUtils.random(6));
System.out.println("RandomStringUtils.randomAscii-->" + RandomStringUtils.randomAscii(6));
System.out.println("RandomStringUtils.randomAlphabetic-->" + RandomStringUtils.randomAlphabetic(6));
System.out.println("RandomStringUtils.randomAlphanumeric-->" + RandomStringUtils.randomAlphanumeric(6));
System.out.println("RandomStringUtils.randomNumeric-->" + RandomStringUtils.randomNumeric(6));
System.out.println("RandomStringUtils.random2-->" + RandomStringUtils.random(6,false, false));
//第一種即是直接寫字元對應的十進制
System.out.println("RandomStringUtils.random3.1-->" + RandomStringUtils.random(6,48,122,false, false));
//第二種是直接寫字元,但要確定字元順序不能寫反
//這裡我将letters和numbers都設定成true,是以生成的随機字元串隻包括字母和數字,這種方法可以使用了,應為在'0'-'z'之間,其他字元已經不多了
System.out.println("RandomStringUtils.random3.2-->" + RandomStringUtils.random(6,'0','z',true, true));
char[] chars = new char[]{'1', '2', '3', 'a', 'b', 'c' };
System.out.println("RandomStringUtils.random4-->" + RandomStringUtils.random(6,0,chars.length,false, false, chars));
String str = "123abc";
System.out.println("RandomStringUtils.random5-->" + RandomStringUtils.random(6, str));
System.out.println("RandomStringUtils.random6-->" + RandomStringUtils.random(6, chars));
}
out:
RandomStringUtils.random1-->븰倉ࠝ饛皾㙨
RandomStringUtils.randomAscii-->ry){(Z
RandomStringUtils.randomAlphabetic-->zTuXYz
RandomStringUtils.randomAlphanumeric-->buP1Nb
RandomStringUtils.randomNumeric-->754463
RandomStringUtils.random2-->왆ሷ䥦ꮽ壠₾
RandomStringUtils.random3.1-->xi<vBW
RandomStringUtils.random3.2-->syQFLj
RandomStringUtils.random4-->3a3ab1
RandomStringUtils.random5-->ac2312
RandomStringUtils.random6-->b1cb3a
if ((letters && Character.isLetter(ch))
|| (numbers && Character.isDigit(ch))
|| (!letters && !numbers))
{}
@Test
public void getSysChildrenMenusTest(){
int count = 100000;
long start = System.currentTimeMillis();
for (int i = 0;i < count; i++){
RandomStringUtils.randomAlphabetic(6);
}
long end = System.currentTimeMillis();
System.out.println("RandomStringUtils.randomAlphabetic-->" + (end-start));
start = System.currentTimeMillis();
for (int i = 0;i < count; i++){
RandomStringUtils.randomNumeric(6);
}
end = System.currentTimeMillis();
System.out.println("RandomStringUtils.randomNumeric-->" + (end-start));
start = System.currentTimeMillis();
for (int i = 0;i < count; i++){
RandomStringUtils.randomAlphanumeric(6);
}
end = System.currentTimeMillis();
System.out.println("RandomStringUtils.randomAlphanumeric-->" + (end-start));
start = System.currentTimeMillis();
for (int i = 0;i < count; i++){
RandomStringUtils.random(6,'0','z',true, true);
}
end = System.currentTimeMillis();
System.out.println("RandomStringUtils.random‘0’-‘z’範圍内-->" + (end-start));
char[] chars = new char[]{'1', '2', '3', '4','5', '6', '7', '8', '9', '0',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',