我正在生成一个随 secret 码。我的密码长度为 8 个字符,其中包含特殊字符。我需要将第一个字母保留为字母表
,并需要打乱剩余的七个字符,使其成为字母数字+ ascii 字符的混合
。
public String generatePassword() {
int passwordLength = MAX_PASSWORD_LENGTH;
StringBuffer password = new StringBuffer(passwordLength);
//first character as an alphabet
password.append(RandomStringUtils.randomAlphabetic(1)).toString();
String alphaNumeric = RandomStringUtils.random(5, true, true);
String asciiChars = RandomStringUtils.randomAscii(2);
password.append(alphaNumeric).append(asciiChars);
return password.toString();
}
我需要一些帮助来打乱最后 7 个字符的顺序。怎么做?
最佳答案
Java Collections API 有一个内置的 shuffle 方法可供您使用:请参阅 here 。
基本上,您需要从最后 7 个字符创建一个 List
,并将其传递给 Collections.shuffle
。
关于java - 随机排列(随机重新排列)随机生成的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11930031/