Java Random String generateRandomString()

Here you can find the source of generateRandomString()

Description

Generate a random String

License

Mozilla Public License

Declaration

public static String generateRandomString() 

Method Source Code

//package com.java2s;
/**/*from   w  ww. j  a v a 2 s. c o  m*/
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import java.util.Random;

public class Main {
    public static Random random = new Random();

    /**
     * Generate a random String
     * 
     * @return
     */
    public static String generateRandomString() {
        String characters = "abcdefghijklmnopqrstuvwxyz123456789";
        int length = generateRandomNumber();
        char[] text = new char[length];
        for (int i = 0; i < length; i++) {
            text[i] = characters.charAt(random.nextInt(characters.length()));
        }
        return new String(text);
    }

    /**
     * Generate a random number between 5 to 16
     * 
     * @return
     */
    public static int generateRandomNumber() {
        Random randomGenerator = new Random();
        return randomGenerator.nextInt(11) + 5;
    }
}

Related

  1. generateRandomString()
  2. generateRandomString()
  3. generateRandomString()
  4. generateRandomString()
  5. generateRandomString()
  6. generateRandomString()
  7. generateRandomString(final int minCharLen, final int maxCharLen)
  8. generateRandomString(final String pattern)
  9. generateRandomString(int count)