Java Random String generateRandomString(int length)

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

Description

generate Random String

License

Apache License

Declaration

public synchronized static String generateRandomString(int length) 

Method Source Code

//package com.java2s;
/*/*  w w  w  . j a  va  2 s  .  com*/
 *    Copyright 2016 Roche NimbleGen 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.Random;

public class Main {
    private static Random RANDOM = new Random(System.currentTimeMillis());
    private static char[] CHARACTERS = new char[] { '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', '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9' };

    public synchronized static String generateRandomString(int length) {
        char[] text = new char[length];
        for (int i = 0; i < length; i++) {
            text[i] = CHARACTERS[RANDOM.nextInt(CHARACTERS.length)];
        }
        return new String(text);

    }
}

Related

  1. generateRandomString()
  2. generateRandomString(final int minCharLen, final int maxCharLen)
  3. generateRandomString(final String pattern)
  4. generateRandomString(int count)
  5. generateRandomString(int len)
  6. generateRandomString(int length)
  7. generateRandomString(int length)
  8. generateRandomString(int length)
  9. generateRandomString(int length)