Java - Write code to generate random String

Requirements

Write code to generate random String

Demo

//package com.book2s;

import java.util.Random;

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

    private static final String PWD_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static Random random = new Random();

    public static String randomString(int len) {
        StringBuilder builder = new StringBuilder(len);
        for (int i = 0; i < len; i++)
            builder.append(PWD_CHARS.charAt(random.nextInt(PWD_CHARS
                    .length())));
        return builder.toString();
    }
}

Related Example