【问题标题】:How to generate random alpha and numeric string如何生成随机字母和数字字符串
【发布时间】:2017-02-08 10:41:59
【问题描述】:

我想生成字母和数字随机密码,我有两个开关,一个用于字母,一个用于数字,当两个开关都打开时,同时生成字母和数字字符串,但当我的密码长度为 4 时时间它只以 alpha 显示,但我想要数字和 alpha。

我的代码如下所示

if (switchLetters.isChecked() && switchDigits.isChecked()) {
    randomString(mProgress, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", txtPassword);
}

String randomString(int len, String mPassword, TextView mTextView) {
    SecureRandom rnd = new SecureRandom();
    StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; i++)
    sb.append(mPassword.charAt(rnd.nextInt(mPassword.length())));
    mTextView.setText(sb.toString());

    return sb.toString();
}

【问题讨论】:

    标签: android random alphanumeric


    【解决方案1】:

    试试下面的代码:

    String randomString(int len, String mPassword, TextView mTextView) {
            SecureRandom rnd = new SecureRandom();
            StringBuilder sb = new StringBuilder(len);
            for (int i = 0; i < len; i++)
                sb.append(mPassword.charAt(rnd.nextInt(mPassword.length())));
    
            mTextView.setText(sb.toString());
            return sb.toString();
        }
    
    
        private String generateAlphaNumericString(int len, String mPassword, TextView mTextView) {
    
            String regexDigit = "\\d+";
            String regexAlphabets = "[a-zA-Z]+";
            String randomAlphaNumString;
    
            do {
                randomAlphaNumString = randomString(mProgress, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", txtPassword);
    
            } while (while ((randomAlphaNumString.matches(regexDigit) || randomAlphaNumString.matches(regexAlphabets)));
            return randomAlphaNumString;
        }
    

    【讨论】:

    • 我的坏。更新了代码。请立即检查。调用 generateAlphaNumericString() 函数。它应该可以工作。
    • 在投票标签(+1)下面你会得到正确的图标,选择那个。
    【解决方案2】:

    Here 是使用 SecureRandom 生成安全随机字母和数字字符串的好例子

    导入 java.security.SecureRandom; 导入 java.math.BigInteger;

    public final class SessionIdentifierGenerator {
      private SecureRandom random = new SecureRandom();
    
      public String nextSessionId() {
        return new BigInteger(130, random).toString(32);
      }
    }
    

    Here 是如何生成具有自定义长度的字符串的示例。

    【讨论】:

    • 但是在这里我如何定义字符串长度,因为字符串长度取决于搜索栏进度!
    • 抱歉,当我的字符串长度为 3 或 4 时,它会显示为 alpha。
    • 一直?这是随机的。它有时只能生成字母
    • 因为它是随机的。它可以是纯字母或纯数字或两者兼而有之。因为有更多不同的字母而不是数字,所以对于小字符串,它可能只有字母。
    • 是的,但是当用户当时只选择 4 个字符串长度时,有时会出现这个问题
    【解决方案3】:

    您在不区分字母和数字的情况下传入要从中选择的字母池。如果要确保两者都包括在内,则需要将它们分开更长时间。

    TextView 参数是不必要的复杂性,属于调用 GUI 代码的某个位置,而不是该实用程序方法。如果需要,您可以重新添加它。

    如果选中switchLetters,则此版本选择一个字母,如果选中switchNumbers,则选择一个数字,确保至少选择每个选定类型中的一个。其余字符是随机的。

    因为前两个字符的顺序是特定的,所以结果会被安全地打乱。这就是为什么我使用数组来保存结果,这样更容易洗牌。

    我已经放入了很多 cmets 来帮助解释事情。

    final String alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    final String digits = "0123456789";
    
    Random secRand = new SecureRandom();
    
    
    String randomString(int len) {
    
        if (len < 2) {
            throw new IllegalArgumentException("randomString - length too short: " + len);
        }
    
        // Pool of characters to select from.
        String pool = "";
    
        // Array to hold random characters.
        Character[] result = new Character[len];
    
        // Index into result.
        int index = 0;
    
        if (switchLetters.isChecked()) {
            // Put letters in pool.
            pool = pool + alpha;
    
            // Ensure at least one letter.
            result[index] = alpha.charAt(secRand.nextInt(alpha.length()));
            index++;
        }
    
        if (switchDigits.isChecked()) {
            // Put digits in pool.
            pool = pool + digits;
    
            // Ensure at least one digit.
            result[index] = digits.charAt(secRand.nextInt(digits.length()));
            index++;
        }
    
        // Fill rest of result array from pool.
        for ( ; index < len; index++) {
            result[index] = pool.charAt(secRand.nextInt(pool.length()));
        }
    
        // Shuffle result array with secRand to hide ordering.
        Collections.shuffle(Arrays.asList(result), secRand);
    
        // Assemble return string.
        StringBuilder sb = new StringBuilder(len);
        for (Character c : result) {
            sb.append(c);
        }
        return sb.toString();
    
    }  // end randomString()
    

    【讨论】:

      猜你喜欢
      • 2021-04-05
      • 2010-09-08
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 2015-11-09
      相关资源
      最近更新 更多