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 static String generateRandomString(int length) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Random;

public class Main {
    private static final char[] RANDOM_CHARS = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ@._()"
            .toCharArray();// w w  w.  j  av  a2s  .  c om

    public static String generateRandomString(int length) {
        return generateRandomString(RANDOM_CHARS, length);
    }

    public static String generateRandomString(char[] randChars, int length) {
        if (length < 1) {
            return null;
        }

        Random rand = new Random();
        int rLen = randChars.length;
        char[] newStr = new char[length];
        for (int i = 0; i < length; i++) {
            newStr[i] = randChars[rand.nextInt(rLen)];
        }

        return new String(newStr);
    }
}

Related

  1. generateRandomString(int length)
  2. generateRandomString(int length)
  3. generateRandomString(int length)
  4. generateRandomString(int length)
  5. generateRandomString(int length)
  6. generateRandomString(int length)
  7. generateRandomString(int length)
  8. generateRandomString(int length)
  9. generateRandomString(int length)