Java Random String getRandomString(int length)

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

Description

get Random String

License

MIT License

Declaration

public static String getRandomString(int length) 

Method Source Code

//package com.java2s;
/*//from   ww  w.ja  va  2  s .c o m
 * oxCore is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

import java.util.Random;

public class Main {
    private static final String charset = "!0123456789abcdefghijklmnopqrstuvwxyz";
    private static Random rand;

    public static String getRandomString(int length) {
        if (rand == null) {
            rand = new Random(System.currentTimeMillis());
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int pos = rand.nextInt(charset.length());
            sb.append(charset.charAt(pos));
        }
        return sb.toString();
    }

    public static String toString(Object[] array) {
        int len = array.length;
        if (len == 0)
            return "";
        StringBuffer buf = new StringBuffer(len * 12);
        for (int i = 0; i < len - 1; i++) {
            buf.append(array[i]).append(", ");
        }
        return buf.append(array[len - 1]).toString();
    }

    public static String toString(Object object) {
        return (object == null) ? null : object.toString();
    }
}

Related

  1. getRandomString(int length)
  2. getRandomString(int length)
  3. getRandomString(int length)
  4. getRandomString(int length)
  5. getRandomString(int length)
  6. getRandomString(int length)
  7. getRandomString(int length)
  8. getRandomString(int length)
  9. getRandomString(int length)