Java Random String getRandomAString(int min, int max)

Here you can find the source of getRandomAString(int min, int max)

Description

get Random A String

License

Open Source License

Declaration

public static String getRandomAString(int min, int max) 

Method Source Code

//package com.java2s;
/*/*from  w  ww  . j  a v a2 s .c  o  m*/
 * *********************************************************************
 * Copyright (c) 2010 Pedro Gomes and Universidade do Minho.
 * All rights reserved.
 * Licensed 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.*;

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

    public static String getRandomAString(int min, int max) {
        String newstring = new String();
        int i;
        final char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
                'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@', '#', '$', '%',
                '^', '&', '*', '(', ')', '_', '-', '=', '+', '{', '}', '[', ']', '|', ':', ';', ',', '.', '?', '/',
                '~' }; //78 characters
        int strlen = (int) Math.floor(rand.nextDouble() * ((max - min) + 1));
        if (strlen < 1)
            strlen = min;
        strlen += min;
        for (i = 0; i < strlen; i++) {
            char c = chars[(int) Math.floor(rand.nextDouble() * 78)];
            newstring = newstring.concat(String.valueOf(c));
        }
        return newstring;
    }

    public static String getRandomAString(int length) {
        String newstring = new String();
        int i;
        final char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
                'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@', '#', '$', '%',
                '^', '&', '*', '(', ')', '_', '-', '=', '+', '{', '}', '[', ']', '|', ':', ';', ',', '.', '?', '/',
                '~', ' ' }; //79 characters
        for (i = 0; i < length; i++) {
            char c = chars[(int) Math.floor(rand.nextDouble() * 79)];
            newstring = newstring.concat(String.valueOf(c));
        }
        return newstring;
    }
}

Related

  1. generateRandomString(String str, int length)
  2. genRandomString()
  3. genRandomString(final int length)
  4. genRandomString(int count)
  5. getDefaultRandomName(String namePrefix)
  6. getRandomColors(final int size, final List colors)
  7. getRandomElementOrCompound(String equation)
  8. getRandomFromTier(int tier, String lvlRange)
  9. getRandomNumberString(int length)