Java Random String getRandomString(int len)

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

Description

Get a random string with the request length

License

Apache License

Parameter

Parameter Description
length of the random string

Declaration

public static String getRandomString(int len) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Random;

public class Main {
    private static final String[] RANDOM_CHARSET = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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" };

    /**//from www  .  j ava2 s.  c o  m
     * Get a random string with the request length
     * 
     * @param length
     *            of the random string
     * 
     * @returns a random string
     */
    public static String getRandomString(int len) {
        Random rand = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            sb = sb.append(RANDOM_CHARSET[rand.nextInt(RANDOM_CHARSET.length)]);
        }
        return sb.toString();
    }
}

Related

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