Java Random String getRandomString(int length)

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

Description

Generates a random string with the provided length.

License

Open Source License

Parameter

Parameter Description
length a parameter

Declaration

public static String getRandomString(int length) 

Method Source Code

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

import java.util.Random;

public class Main {
    /**/*from ww w. jav a 2s .  c o m*/
     * Generates a random string with the provided length.
     * 
     * @param length
     * @return
     */
    public static String getRandomString(int length) {
        StringBuffer retObj = new StringBuffer();
        String letters = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789";

        Random random = new Random(System.currentTimeMillis());

        for (int i = 0; i < length; i++) {
            int pos = random.nextInt(letters.length());
            retObj.append(letters.charAt(pos));
        }
        return retObj.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, Random rnd)
  9. getRandomString(int length, String charset)