Java Random String genRandomString(int count)

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

Description

gen Random String

License

Open Source License

Declaration

public static String genRandomString(int count) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Random;

public class Main {
    private static final char[] DEFAULT_CHARS = new char[] { '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', };

    public static String genRandomString(int count) {
        return genRandomString(count, DEFAULT_CHARS);
    }//w  ww .j av  a 2 s  . co  m

    public static String genRandomString(int count, char[] elements) {
        StringBuffer sb = new StringBuffer();

        int len = elements.length - 1;
        for (int i = 0; i < count; i++) {
            sb.append(elements[new Random(System.nanoTime()).nextInt(len)]);
        }

        return sb.toString();
    }
}

Related

  1. generateRandomString(Random random, int size)
  2. generateRandomString(Random rnd, char[] alphabet, int maxLength)
  3. generateRandomString(String str, int length)
  4. genRandomString()
  5. genRandomString(final int length)
  6. getDefaultRandomName(String namePrefix)
  7. getRandomAString(int min, int max)
  8. getRandomColors(final int size, final List colors)
  9. getRandomElementOrCompound(String equation)