generate Password - Java java.lang

Java examples for java.lang:String Random

Description

generate Password

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    public static void main(String[] argv) {
        System.out.println(generatePassword());
    }/*from  ww w . j a v a2  s.c  o m*/

    public static String generatePassword() {
        return generateString(
                "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
                12);
    }

    public static String generateString(String characters, int length) {
        Random rng = new Random(System.nanoTime());
        char[] text = new char[length];
        for (int i = 0; i < length; i++) {
            text[i] = characters.charAt(rng.nextInt(characters.length()));
        }
        return new String(text);
    }
}

Related Tutorials