通过commons-text
工具类,生成指定长度随机数。
引入maven
1 2 3 4 5
| <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.4</version> </dependency>
|
工具使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
public class RandomHelper {
public static String randomNumeric(int count) { RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange('0', '9').build(); return generator.generate(count); }
public static String randomAlphabetic(int count) { char [][] pairs = {{'a','z'},{'A','Z'}}; RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange(pairs).build(); return generator.generate(count); }
public static String randomStr(int count) { char [][] pairs = {{'a','z'},{'0','9'},{'A','Z'}}; RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange(pairs).build(); return generator.generate(count); }
}
|