Java Random String generateRandomString(final int minCharLen, final int maxCharLen)

Here you can find the source of generateRandomString(final int minCharLen, final int maxCharLen)

Description

generate Random String

License

Open Source License

Declaration

public static String generateRandomString(final int minCharLen, final int maxCharLen) 

Method Source Code

//package com.java2s;
/**//from   w  w  w. j a  va  2  s . co  m
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 netirc.org development team
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.Random;

public class Main {
    private static String symbols;

    public static String generateRandomString(final int minCharLen, final int maxCharLen) {
        if (minCharLen > maxCharLen) {
            throw new IllegalArgumentException(
                    "Maximum Character Length must be larger than the Minimum Character Length");
        }

        StringBuilder builder = new StringBuilder();
        generateCharacterList();
        int len = (int) (Math.random() * (minCharLen - maxCharLen)) + minCharLen;

        Random r = new Random();
        final int randomMax = symbols.length();

        for (int i = 0; i < len; i++) {
            builder.append(symbols.charAt(r.nextInt(randomMax)));
        }

        return builder.toString();
    }

    protected static void generateCharacterList() {
        if (symbols != null) {
            return;
        }

        StringBuilder tmp = new StringBuilder();

        for (char ch = '0'; ch <= '9'; ++ch) {
            tmp.append(ch);
        }
        for (char ch = 'a'; ch <= 'z'; ++ch) {
            tmp.append(ch);
        }
        for (char ch = 'A'; ch <= 'Z'; ++ch) {
            tmp.append(ch);
        }

        symbols = tmp.toString();
    }
}

Related

  1. generateRandomString()
  2. generateRandomString()
  3. generateRandomString()
  4. generateRandomString()
  5. generateRandomString()
  6. generateRandomString(final String pattern)
  7. generateRandomString(int count)
  8. generateRandomString(int len)
  9. generateRandomString(int length)