Java Random String getRandomString(int len)

Here you can find the source of getRandomString(int len)

Description

Generate a string of random characters of the given length.

License

Open Source License

Parameter

Parameter Description
len Length of the random string

Return

String of random characters

Declaration

public static String getRandomString(int len) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    /** List of characters to use in random string generator */
    private static final String RANDOM_CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**//from www  .j av  a2  s  .c  o m
     * Generate a string of random characters of the given length.
     *
     * @param   len   Length of the random string
     * @return  String of random characters
     */
    public static String getRandomString(int len) {
        Random rnd = new Random();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            sb.append(RANDOM_CHARACTERS.charAt(rnd.nextInt(RANDOM_CHARACTERS.length())));
        }
        return sb.toString();
    }
}

Related

  1. getRandomString(final int size)
  2. getRandomString(int cantidad, boolean mayusculas, boolean minusculas, boolean numeros, boolean simbolos, boolean repetir)
  3. getRandomString(int count)
  4. getRandomString(int len)
  5. getRandomString(int len)
  6. getRandomString(int len)
  7. getRandomString(int len, boolean ascii_only)
  8. getRandomString(int lenght)
  9. getRandomString(int length)