Java Random String getRandomString(int minLength, int maxLength)

Here you can find the source of getRandomString(int minLength, int maxLength)

Description

get Random String

License

Open Source License

Declaration

public static String getRandomString(int minLength, int maxLength) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Robert "Unlogic" Olofsson (unlogic@unlogic.se).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 ******************************************************************************/

import java.util.Random;

public class Main {
    private static final Random RANDOM = new Random();

    public static String getRandomString(int minLength, int maxLength) {

        int length;

        if (minLength == maxLength) {

            length = minLength;//  w  w w .ja  v a  2s .  c  om

        } else {

            length = RANDOM.nextInt(maxLength - minLength) + minLength;
        }

        char[] randomString = new char[length];

        for (int x = 0; x < length; x++) {
            int randDecimalAsciiVal = RANDOM.nextInt(25) + 97;
            randomString[x] = (char) randDecimalAsciiVal;
        }

        return new String(randomString);
    }
}

Related

  1. getRandomString(int length)
  2. getRandomString(int length)
  3. getRandomString(int length, Random rnd)
  4. getRandomString(int length, String charset)
  5. getRandomString(int min, int max)
  6. getRandomString(int randomPasswordLength)
  7. getRandomString(int size)
  8. getRandomString(int size)
  9. getRandomString(int size)