generate Random Password - Android java.util

Android examples for java.util:Random

Description

generate Random Password

Demo Code

import java.security.SecureRandom;
import java.util.Random;

public class Main{


    private static final Random RANDOM = new SecureRandom();

    public static final char[] RANDOM_LETTERS = new char[] { 'a', 'b', 'c',
            'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
            'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',
            'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
            'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+',
            '-', '@' };

    public static String generateRandomPassword(int length) {

        StringBuffer sb = new StringBuffer(length);
        for (int i = 0; i < length; i++) {
            int index = (int) (RANDOM.nextDouble() * RANDOM_LETTERS.length);
            sb.append(RANDOM_LETTERS[index]);
        }/*w w w .  j a v a2  s. c  om*/
        return sb.toString();
    }

}

Related Tutorials