Java Random String getRandomString(int count)

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

Description

get Random String

License

Apache License

Declaration

public static String getRandomString(int count) 

Method Source Code

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

import java.util.Random;

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

    public static String getRandomString(int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++)
            sb.append(getRandomChar());/* w  w w. ja  va 2  s .  c  om*/
        return sb.toString();
    }

    public static String getRandomString(int count, boolean withInt) {
        StringBuilder sb = new StringBuilder();
        if (withInt)
            for (int i = 0; i < count; i++)
                sb.append(getRandomChar());
        else
            for (int i = 0; i < count; i++)
                sb.append(getRandomCharWithoutInt());

        return sb.toString();
    }

    private static String getRandomChar() {
        Random i = new Random();
        return chars[i.nextInt(chars.length)];
    }

    private static String getRandomCharWithoutInt() {
        Random i = new Random();
        return chars[i.nextInt(26)];
    }
}

Related

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