java中的随机唯一字母数字字符串



我想生成一个由大写和小写字符组成的字符串但它们应该是唯一的,具有特定的长度java中是否有生成唯一字符串的算法或库?

Apache公共语言库是一个很好的库,用于创建随机数和许多更有用的东西。

您可以创建一个包含字母和数字的随机字符串,如下所示。

1获取依赖关系。在等级上它是-

compile group: 'org.apache.commons', name: 'commons-lang3'

2个进口随机工具

import org.apache.commons.lang3.RandomStringUtils;

3生成大小为8的随机字符串,字母=true,数字=true

RandomStringUtils.random(8, true, true)
public class Main {
public static void main(String[] args) {
char[] array = "1234567890qwertyuiopasdfghjklzxcvbnm".toCharArray();
String randomString = null;
for (int x = 0; x < 10; x++;) {
randomString += "" + array[0 + (int)(Math.random() * ((10 - 0) + 1))];
}
System.out.println("Your string is: " + randomString);
}
}

这应该行得通。

最新更新