使用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)) {}
很明顯,只要letters和numbers有一個為true,其所生成的字符就必須被檢驗是否為字母或數字,所以當不限定搜索范圍時,不建議使letters和numbers為true。如API中的3、4、5。
做一個效率測試:
@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', 'm', 'n'