mercredi 8 février 2017

How to generate random alpha and numeric string

I want to generate alpha and numeric random password, i have two switches, one for alpha and one for numeric when both switches are on at that time generated string in alpha and numeric also but when my password length will be 4 so some time it's showing only in alpha but i want in digit and also in alpha.

my code is look like below

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();
    }




Aucun commentaire:

Enregistrer un commentaire