get Random A String - Java java.util

Java examples for java.util:Random String

Description

get Random A String

Demo Code

/*/* ww  w. 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.
 *
 * ********************************************************************
 */
//package com.java2s;

import java.util.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        int length = 2;
        System.out.println(getRandomAString(length));
    }

    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 Tutorials