Here you can find the source of getRandomString(Random rnd, int minLength, int maxLength)
Parameter | Description |
---|---|
rnd | The random used to create the strings. |
minLength | The minimum string length. |
maxLength | The maximum string length (inclusive). |
public static String getRandomString(Random rnd, int minLength, int maxLength)
//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); } }