Java Random String generateRandomString(int n)

Here you can find the source of generateRandomString(int n)

Description

generate Random String

License

Apache License

Declaration

public static String generateRandomString(int n) 

Method Source Code

//package com.java2s;
/*//w w  w . ja v a 2 s .  co m
 * Copyright 2012 Nodeable Inc
 *
 *    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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Main {
    public static String generateRandomString(int n) {

        Random rd = new Random();

        char lowerChars[] = "abcdefghijklmnopqrstuvwxyz".toCharArray();
        char upperChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
        char numbers[] = "0123456789".toCharArray();
        //char specialChars[] = "~!@#$%^&*()-_=+[{]}|;:<>/?".toCharArray();
        char specialChars[] = "!$-_+".toCharArray(); // limited subset

        List<Character> pwdLst = new ArrayList<>();
        for (int g = 0; g <= n; g++) {
            for (int z = 0; z < 1; z++) {
                if (g == 0) {
                    pwdLst.add(numbers[rd.nextInt(10)]); // must match char array length(s) above
                } else if (g == 1) {
                    pwdLst.add(lowerChars[rd.nextInt(26)]);
                } else if (g == 2) {
                    pwdLst.add(upperChars[rd.nextInt(26)]);
                } else if (g == 3) {
                    pwdLst.add(specialChars[rd.nextInt(5)]);
                }
            }
            if (pwdLst.size() == n) {
                break;
            }
            if (g + 1 == 4) {
                g = (int) (Math.random() * 5);

            }
        }
        StringBuilder password = new StringBuilder();
        Collections.shuffle(pwdLst);
        for (Character aPwdLst : pwdLst) {
            password.append(aPwdLst);
        }
        return password.toString();
    }

    /**
     * Generate a random string, default is 10 characters in length
     *
     * @return
     */
    public static String generateRandomString() {
        return generateRandomString(10);
    }
}

Related

  1. generateRandomString(int length)
  2. generateRandomString(int Length, String caseType)
  3. generateRandomString(int length, String charset)
  4. generateRandomString(int minLength, int maxLength, int minLCaseCount, int minUCaseCount, int minNumCount, int minSpecialCount)
  5. generateRandomString(int n)
  6. generateRandomString(int size)
  7. generateRandomString(int stringLength)
  8. generateRandomString(int wordsLength, String separator)
  9. generateRandomString(Random random, int length)