Java Random String getRandomString(Random rnd, int minLength, int maxLength)

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

Description

Creates a random string with a length within the given interval.

License

Apache License

Parameter

Parameter Description
rnd The random used to create the strings.
minLength The minimum string length.
maxLength The maximum string length (inclusive).

Return

A random String.

Declaration

public static String getRandomString(Random rnd, int minLength,
        int maxLength) 

Method Source Code

//package com.java2s;
/*//w  ww  . ja va2s  . c o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Random;

public class Main {
    /**
     * Creates a random string with a length within the given interval. The string contains only characters that
     * can be represented as a single code point.
     * 
     * @param rnd The random used to create the strings.
     * @param minLength The minimum string length.
     * @param maxLength The maximum string length (inclusive).
     * @return A random String.
     */
    public static String getRandomString(Random rnd, int minLength,
            int maxLength) {
        int len = rnd.nextInt(maxLength - minLength + 1) + minLength;

        char[] data = new char[len];
        for (int i = 0; i < data.length; i++) {
            data[i] = (char) (rnd.nextInt(0x7fff) + 1);
        }
        return new String(data);
    }

    /**
     * Creates a random string with a length within the given interval. The string contains only characters that
     * can be represented as a single code point.
     * 
     * @param rnd The random used to create the strings.
     * @param minLength The minimum string length.
     * @param maxLength The maximum string length (inclusive).
     * @param minValue The minimum character value to occur.
     * @param maxValue The maximum character value to occur.
     * @return A random String.
     */
    public static String getRandomString(Random rnd, int minLength,
            int maxLength, char minValue, char maxValue) {
        int len = rnd.nextInt(maxLength - minLength + 1) + minLength;

        char[] data = new char[len];
        int diff = maxValue - minValue + 1;

        for (int i = 0; i < data.length; i++) {
            data[i] = (char) (rnd.nextInt(diff) + minValue);
        }
        return new String(data);
    }
}

Related

  1. getRandomString(int size)
  2. getRandomString(int size)
  3. getRandomString(int strLength)
  4. getRandomString(Random random, int minLen, int maxLen)
  5. getRandomString(Random random, int totalLength, int randomSectionLength)
  6. getRandomString(String paramString, int paramLength)
  7. getRandomStringByLength(int length)
  8. getRandomStringForDate()
  9. getRandomStringOfLetters(int length)